I have a String, which I want to split into parts using delimeter }},{". I have tried using:
String delims="['}},{\"']+";
String field[]=new String[50];
field=subResult.split(delims);
But it is not working :-( do you know, what expression in delims should I use?
Thanks for your replies
A { is a regex meta-character which marks the beginning of a character class. To match a literal { you need to escape it by preceding it with a \\ as:
String delims="}},\\{";
String field[] = subResult.split(delims);
You need not escape the } in your regex as the regex engine infers that it is a literal } as it is not preceded by a opening {. That said there is no harm in escaping it.
See it
If the delimiter is simply }},{ then subResult.split("\\}\\},\\{") should work
String fooo = "asdf}},{bar}},{baz";
System.out.println(Arrays.toString(fooo.split("\\}\\},\\{")));
You should be escaping it.
String.split("\\}\\},\\{");
You could be making it more complex than you need.
String text = "{{aaa}},{\"hello\"}";
String[] field=text.split("\\}\\},\\{\"");
System.out.println(Arrays.toString(field));
Use:
Pattern p = Pattern.compile("[}},{\"]");
// Split input with the pattern
String[] result = p.split(MyTextString);
Related
i want a Regex expression to split a string based on \r characters not a carriage return or a new line.
Below is the sample string i have.
MSH|^~\&|1100|CB|CERASP|TESTSB8F|202008041554||ORU|1361|P|2.2\rPID|1|833944|21796920320|8276975
i want this to be split into
MSH|^~\&|1100|CB|CERASP|TESTSB8F|202008041554||ORU|1361|P|2.2
PID|1|833944|21796920320|8276975
currently i have something like this
StringUtils.split(testStr, "\\r");
but it is splitting into
MSH|^~
&|1100|CB|CERASP|TESTSB8F|202008041554||ORU|1361|P|2.2
PID|1|833944|21796920320|8276975
You can just use String#split:
final String str = "MSH|^~\\&|1100|CB|CERASP|TESTSB8F|202008041554||ORU|1361|P|2.2\\rPID|1|833944|21796920320|8276975";
final String[] substrs = str.split("\\\\r");
System.out.println(Arrays.toString(substrs));
// Outputs [MSH|^~\&|1100|CB|CERASP|TESTSB8F|202008041554||ORU|1361|P|2.2, PID|1|833944|21796920320|8276975]
You can use
import java.utl.regex.*;
//...
String[] results = text.split(Pattern.quote("\\r"));
The Pattern.quote allows using any plain text inside String.split that accepts a valid regular expression. Here, \ is a special char, and needs to be escaped for both Java string interpretation engine and the regex engine.
The method being called matches any one of the contents in the delimiter string as a delimiter, not the entire sequence. Here is the code from SeparatorUtils that executes the delimiter (str is the input string being split) check:
if (separatorChars.indexOf(str.charAt(i)) >= 0) {
As #enzo mentioned, java.lang.String.split() will do the job - just make sure to quote the separator. Pattern.quote() can help.
Fairly new to regular expressions. I want to be able to remove all non-alphanumeric characters besides $.
So for the string like "I am here $today#", the result should be "I am here $today"
I have tried this already with no luck.
[^a-zA-Z\\s$] and [$^a-zA-Z\\s] and [^a-zA-Z$\\s]
String regex = "[^a-zA-Z\\s$]";
String string = "I am here $today#";
string = string.replaceAll(regex, "");
System.out.println(string); // I am here $today
This is working just fine...
This might help, Use:
replaceAll("[^\\w\\s\\$]", "");
\w is short for [a-zA-Z_0-9]
I have this kind of String ('123','12345678') and i would validate it throw a regex.
I have write this code, but i'm not shure it work.
String field = "('123','12345678')";
String regex = "^('\\d{3}','\\d{8}')$";
public void valideField(String field, String regex){
{
if(!field.matches(regex)){
System.out.println("Not validated!");
}
}
Is correct regex or not? You have any suggestions or help?
You need to escape parentheses (using backslashes) as they represent capturing groups in regular expressions:
String regex = "^\\('\\d{3}','\\d{8}'\\)$";
You have to escape the single quotes and parenthesis too with \\' and \\(
The correct regex would be:
String regex = "\\(\\'\\d{3}\\',\\'\\d{8}\\'\\)";
In my project I used the code to split string like "004*034556" , code is like below :
String string = "004*034556";
String[] parts = string.split("*");
but it got some error and force closed !!
finally I found that if use "#" or another things its gonna work .
String string = "004#034556";
String[] parts = string.split("#");
how can I explain this ?!
Your forgetting something very trivial.
String string = "004*034556";
String[] parts = string.split("\\*");
I recommend you check out Escape Characters.
Use Pattern.quote to treat the * like the String * and not the Regex * (that have a special meaning):
String[] parts = string.split(Pattern.quote("*"));
See String#split:
public String[] split(String regex)
↑
Refer JavaDoc
String[] split(String regex)
Splits this string around matches of the given regular expression.
And the symbol "*" has a different meaning when we talk about Regex in Java
Thus you would have to use an escape character
String[] parts = string.split("\\*");
I want to split a string "ABC\DEF" ?
I have tried
String str = "ABC\DEF";
String[] values1 = str.split("\\");
String[] values2 = str.split("\");
But none seems to be working. Please help.
String.split() expects a regular expression. You need to escape each \ because it is in a java string (by the way you should escape on String str = "ABC\DEF"; too), and you need to escape for the regex. In the end, you will end with this line:
String[] values = str.split("\\\\");
The "\\\\" will be the \\ string, which the regex will interpret as \.
Note that String.split splits a string by regex.
One correct way1 to specify \ as delimiter, in RAW regex is:
\\
Since \ is special character in regex, you need to escape it to specify the literal \.
Putting the regex in string literal, you need to escape again, since \ is also escape character in string literal. Therefore, you end up with:
"\\\\"
So your code should be:
str.split("\\\\")
Note that this splits on every single instance of \ in the string.
Footnote
1 Other ways (in RAW regex) are:
\x5C
\0134
\u005C
In string literal (even worse than the quadruple escaping):
"\\x5C"
"\\0134"
"\\u005C"
Use it:
String str = "ABC\\DEF";
String[] values1 = str.split("\\\\");
final String HAY = "_0_";
String str = "ABC\\DEF".replace("\\", HAY);
System.out.println(Arrays.asList(str.split(HAY)));