Regular Expression for EDI file [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to get a value in a edi file having below format
\nRJCK3:0*20180105*U*127.35
\nRJCK3:0*20180105*B*127.35
I want a value U in 1st case, which comes between 2nd & 3rd star after RJC*K3, and want B in second string
Precisely, want to fetch a single character from a string, where that character will comes between 2nd & 3rd star(*) of a RJC*K3(static value).

You can use a classic Pattern matching way :
String str1 = "\\nRJC*K3:0*20180105*U*127.35";
Matcher m = Pattern.compile("RJC\\*K3.*\\*(\\w)\\*.*").matcher(str1);
String res1 = m.find() ? m.group(1) : "";
System.out.println(res1); // U
But if there is always the same amount of * before the letter you want, you cn easily split and take the 3rd part :
String str2 = "\\nRJC*K3:0*20180105*G*127.35";
String res2 = str2.split("\\*")[3];
System.out.println(res2); // G

There is no need to fight with edi files, you can use available libraries.
Please take a look at https://github.com/imsweb/x12-parser/
RJCloop.getSegment("RJC").getElementValue("RJC02")
can get you the value needed.

Related

Regex to remove prefix and suffix in a string Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to remove prefix and suffix in a String and extract the middle portion of the string.
For eg: Consider the Strings - "www.hello.com" and "www.test.com"
Here prefix - "www." and suffix - ".com". I want to extract the middle words - hello and test.
Currently i have achieved this using String.replace() method in Java.
str.replace("www.","").replace(".com","");
I want know is there any regular expression available to achieve it in a single method in java.
You could use a regex for that, it would work in the same way. Your regex would simply contain a capture group with both the prefix and the suffix in an OR operation.
(www\.|\.com)
You could then use this like you did with the replace.
String test = "www.test.com";
String output = test.replaceAll("(www\\.|\\.com)","")
P.S. this code is untested. Please don't just copy and paste it expecting everything to work.
(?<=www.)(.*)(?=.com)
This uses the lookbehind and lookahead feature of regex

Regular expression for a String to mask digits except last 4 in xml tag content [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
this is an account number 77787845456464645656547877
accountNumber1.replaceAll( "(desc>||desc\s*[:]{2}\s*?|)(\d(?=\d{4}))", "$1XXXXXXXXXX$2") This is not working,I tried different ways no result .Pl check ,thx
An approach without regex:
char[] cs = accountNum.toCharArray();
Arrays.fill(cs, 0, cs.length-4, '*');
String masked = new String(cs);
One approach is to replace every digit with a *, provided it's followed by four or more other digits. Do the last part with a forward lookup, something like
myString.replaceAll("\\d(?=\\d{4})", "*");
which returns myString with every digit other than the last four replaced with *.

Java-To insert a substring into a main string after a given substring of main String [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
There are 3 text fields the user will type into
"To be inserted message" in first Text field
"The message" in second Text field
and a substring of the message in the third text field
The program should be able to insert "To be inserted message" in Message after the substring.
Example ↓
Message→ Stack flow
To be inserted message → over
substring→Stack
Output →
Stack over flow
When do you want to insert substring into the main string you can use insert(int offset, A). But before you need to get the place where you need to put it and we need to REMEMBER that insert() works with StringBuilder. When you don't use it you can use method substring(). From you example, as can be seen, you will point after substring. To do this, we need to find that substring into the main string whereby: indexOf(String str).
Assuming we have: mainSTR, subSTR and str after which we need to input subSTR.
String mainSTR = "Stack flow";
String subSTR = "over";
String str = "Stack";
Let's go to think!
int pos_str = mainSTR.indexOf(str)+str.length();
pos_str will point us for place. Also, for first input str we added its length. It needed to point the space after "Stack". Now we can input subSTR in mainSTR.
Here is an answer for you question: Insert a character in a string at a certain position
Using substring() you need to take, at first, first part of main string, later add subSTR and later add second part of mainSTR.

How to get a character index in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get a character index in a java.lang.String. So, for example, I want to find the '2' character index in the "0123456789" string (the index will be 2). There is any method in the String class to use for this, or a simple code?
Thanks for the answer!
You can iterate through the String and look for a character you seek.
Just use a for loop like this:
String test = "I want to test something";
for(int i=0;i<test.length;i++) {
char t = test.charAt(i);
// do something with char
}
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt%28int%29
index will be -1 if 'a' is not in this string.
Note this code will only find the first 'a' in this string. If you need to find multiple locations of a character, then loop through the string using solution in another answer.
String str = "there is a letter";
int index = str.indexOf('a');

need a way to print a string in binary format? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string. It contains some blank space. I don't know what's these blank space. They might be a space, or be a tab.. or sth else.
So I just want to print a string in binary format.
Also, how to use String.replace() to filter out some characters in binary format? Or I need to use some other kind of API to filter out the special characters in binary format.
for example, how to filter out chr(10) in a string. ( chr(10) means a Chang Line)
Just find out its reason:
I use these line to print what's this whitespace:
for (char ch : MyString.toCharArray()) {
System.out.format("%H ", ch);
}
I got it's result. It's "3000".
I checked UTF-8 table and found 3000 really looks a blank space.
Then I just use this code to remove this blank space:
MyString = MyString.replaceAll("\u3000", "");
Now everything become ok.
Thanks a lot for everyone's help!!
Use regex. \s represents whitespace.
str.replace( "\\s+", "" );
Use String.toCharArray(), loop over all the char, use String.toString((int) char), 16) to get a hex value. Separate with " " or "," for readability.

Categories