how to split a string which contains of ( \n : , .) - java

so how can split this combination in android ?
Thanks in advance :)`
i'm trying like thatString lines[] = String.split("\\r?\\n", -1);
but how can split all data in one time

You can use Pattern for regex split
String fields = "name[Employee Name], employeeno[Employee No], dob[Date of
Birth], joindate[Date of Joining]";
Pattern pattern = Pattern.compile("\\[.+\\]+?,?\\s*" );
String[] split = pattern.split(fields);
References: How to split this string using Java Regular Expressions

Related

Regex split not working

I want split my string using regex.
String Str = " Dřevo5068Hlína5064Železo5064Obilí4895";
String reg = "(\\D+)(\\d+)(\\D+)(\\d+)(\\D+)(\\d+)(\\D+)(\\d+)";
if (Str.matches(reg)) {
String[] l = Str.split(reg);
System.out.println(Arrays.toString(l));
}
But, output is []. Where is problem?
Edit: I want split to:
Dřevo
5068
Hlína
5064
Železo
5064
Obilí
4895
Then I want get numbers from this String.
if your engine permits look-around, split using this pattern
(?<=\D)(?=\d)|(?<=\d)(?=\D)
Demo

How to split a string into two parts on specific delimeter

I have a string "Rush to ER/F07^e80c801e-ee37-4af8-9f12-af2d0e58e341".
I want to split it into 2 strings on the delimiter ^. For example string str1=Rush to ER/F07 and String str2 = e80c801e-ee37-4af8-9f12-af2d0e58e341
For getting this i am doing splitting of the string , I followed the tutorial on stackoverflow but it is not working for me , here is a code
String[] str_array = message.split("^");
String stringa = str_array[0];
String stringb = str_array[1];
when I am printing these 2 strings I am getting nothing in stringa and in stringb I am getting all the string as it was before the delimiter.
Please help me
You have to escape special regex sign via \\ try this:
String[] str_array = message.split("\\^");
It is because the .split() method requires a regex pattern. Escape the ^:
String[] str_array = message.split("\\^");
You can get more information on this at http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-.

Giving inputs to java regex

I have a regex like below one :
"\\t'AUR +(username) .*? /ROLE=\"(my_role)\".*$"
username and my_role parts will be given from args. So they always change when the script is starting. So how can i give parameters to that part of regex ?
Thanks for your helps.
Define regex like this:
String fmt = "\\t'AUR +(%s) .*? /ROLE=\"(%s)\".*$";
// assuming userName and myRole are your arguments
String regex = String.format(fmt, userName, myRole);
You should escape special characters in dynamic strings using Pattern.quote. To put the regex parts together you can simply use string concatenation like this:
String quotedUsername = Pattern.quote(username);
String quotedRole = Pattern.quote(my_role);
String regexString = "\\t'AUR +(" + quotedUsername +
") .*? /ROLE=\"(" + quotedRole + ")\".*$";
I think mixing regular expressions with format strings when using String.format can make the regex harder to understand.
Use string format or straight string concat to construct the regex before passing it to compile ...
Try this for an example:
String patternString = "\\t'AUR +(%s) .*? /ROLE=\"(%s)\".*$";
String formatted = String.format(patternString, username,my_role);
System.out.println(formatted);
Pattern pattern = Pattern.compile(patternString);
You can run a working example here: http://ideone.com/93YeNg

regex: Java: match word between 2 spaces

How can I extract the "id" from the following string using regex.
string = 11,"col=""book"" id=""title"" length=""10""
I need to be able to extract the "id" header along with the value "title".
outcome: id=""title""
I am trying to the use split function with a regex to extract the identifier from the string.
Try this:
String result = "col=\"book\" id=\"title\" length=\"10\"";
String pattern = ".*(id\\s*=\\s*\"[^\"]*\").*";
System.out.println(result.replaceAll(pattern,"$1"));
Cheers!
Use Pattern and Matcher classes to find what you are looking for. Try to find these regex \\bid=[^ ]*.
String data = "string = 11,\"col=\"\"book\"\" id=\"\"title\"\" length=\"\"10\"\"";
Matcher m = Pattern.compile("\\bid=[^ ]*").matcher(data);
if (m.find())
System.out.println(m.group());

Escape special characters in java

I have a text file having | (pipe) as the separator. If I am reading a column and the column itself also contains | then it while separating another column is created.
Example :
name|date|age
zzz|20-03-22|23
"xx|zz"|23-23-33|32
How can I escape the character within the double quotes ""
how to escape the regular expression used in the split, so that it works for user-specified delimiters
i have tried
String[] cols = line.split("\|");
System.out.println("lets see column only=="+cols[1]);
How can I escape the character within the double quotes ""
Here's one approach:
String str = "\"xx|zz\"|23-23-33|32";
Matcher m = Pattern.compile("\"[^\"]*\"").matcher(str);
StringBuffer sb = new StringBuffer();
while (m.find())
m.appendReplacement(sb, m.group().replace("|", "\\\\|"));
m.appendTail(sb);
System.out.println(sb); // prints "xx\|zz"|23-23-33|32
In order to get the columns back you'd do something like this:
String str = "\"xx\\|zz\"|23-23-33|32";
String[] cols = str.split("(?<!\\\\)\\|");
for (String col : cols)
System.out.println(col.replace("\\|", "|"));
Regarding your edit:
how to escape the regular expression used in the split, so that it works for user-specified delimiters
You should use Pattern.quote on the string you want to split on:
String[] cols = line.split(Pattern.quote(delimiter));
This will ensure that the split works as intended even if delimiter contains special regex-symbols such as . or |.
You can use a CSV parser like OpenCSV ou Commons CSV
http://opencsv.sourceforge.net
http://commons.apache.org/sandbox/csv
You can replace it with its unicode sequence (prior to delimiting with pipe)
But what you should do is adjust your parser to take that into account, rather than changing the files.
Here is one way to parse it
String str = "zzz|20-03-22|23 \"xx|zz\"|23-23-33|32";
String regex = "(?<=^|\\|)(([^\"]*?)|([^\"]+\"[^\"]+\".*?))(?=\\||$)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println(m.group());
}
Output:
zzz
20-03-22
23 "xx|zz"
23-23-33
32

Categories

Resources