Java split string with regex, anything inside Double Quotes [duplicate] - java

This question already has answers here:
Regex for splitting a string using space when not surrounded by single or double quotes
(16 answers)
Closed 2 years ago.
I am trying to split a string such as String s = "do not split this \"split this\"";
String[] split = s.split("(?<=\\s)| (?=\") | ((?=[^A-Za-z0-9])|(?<=[^A-Za-z0-9]));
will give me ["do", " ", "not", " ", "split", "this", " ", "split this"];
I would like to keep all words, white spaces as well, but ignore anything inside double quotes~

just a guess:
String s = "do not split this \"split this\"";
String[] split = s.split( "(?<!\".{0,255}) | (?!.*\".*)" ); // do, not, split, this, "split this"
don't split at the blank, if the blank is surrounded by quotes
split at the blank when the 255 characters to the left and all characters to the right of the blank are no quotes

Related

Java split String that start with space [duplicate]

This question already has answers here:
How to prevent java.lang.String.split() from creating a leading empty string?
(9 answers)
Closed 4 years ago.
I know how to split a string by space as the following:
String[] array = string.split(" ");
This works great until I try to split string that starts with a space like
" I like apple"
The result looks something like this:
{"", "I", "like", "apple"}
How can I split the string so it only keeps strings that is not empty?
You can call string.trim()and then string.split(" "). The trim() method removes spaces before the first non-space-character and after the last non-space-character.
To remove leading and trailing spaces, you can use .trim().
String[] array = string.trim().split(" ");

Android: Split string by "\n" as string not as new line [duplicate]

This question already has answers here:
How to split a java string at backslash
(8 answers)
Closed 4 years ago.
I have a below string which is coming from the server
String text = "- 30016264\n- 30014837\n- 30014836\n";
When I used to split it like this
String[] list = text.split("\n");
I got the list like this with length 1
list[0] = "- 30016264\n- 30014837\n- 30014836\n";
And when I used to split it like this
String[] list = text.split("\\n");
I got the same list like this with length 1
list[0] = "- 30016264\n- 30014837\n- 30014836\n";
How do I write the code to split the string on basis of "\n" not the next line?
NOTE: This string is coming from the server as it is written here and when I use this server string as TextView value, it will display in one single line.
If you input is coming from the server and in this format :
- 30016264\n- 30014837\n- 30014836\n
Then, in Java it should be represented with double backslash like this :
- 30016264\\n- 30014837\\n- 30014836\\n
because backslash is a special character in Java, you have to escape it with another backslash.
Then to split with \\n you need to use \\\\n, why 4 backslashes because like i said before the backslash is special character for that you have to escape each one with another backslash for that you need 4 instead of 2 or 1.
Your solution should look like :
String text = "- 30016264\\n- 30014837\\n- 30014836\\n";
String[] split = text.split("\\\\n");
Outputs
- 30016264
- 30014837
- 30014836

Replace spaces in string with regex in Java [duplicate]

This question already has answers here:
Java regular expression to match _all_ whitespace characters
(7 answers)
Closed 8 years ago.
I'm trying to replace all all spaces in string with one space. I'm trying this:
String src = "2.       Test Sentence with spaces";
String out = src.replaceAll("\\s+", " ");
System.out.println(out);
And this is what I'm getting:
2.       Test Sentence with spaces
Spaces after dot were not replaced... Why?
You can try with the Unicode category: separator, space, combined with whitespace:
String input = "\u0020\u00A0\u1680\u2000\u2001\t"; //etc. 17 characters
System.out.println(input.replaceAll("[\\p{Zs}\\s]+", " "));
Output
[1 space]
See here for the list of characters in category Zs.

How do I replace white spaces to nbsp? [duplicate]

This question already has answers here:
Java Regex that only replaces multiple whitepaces with Non-Breaking Spaces
(3 answers)
Closed 8 years ago.
How do I convert any white spaces to nbsp? This doesn't seem to work for me:
String temp = "This is practice";
temp = temp.replaceAll(" ", "nbsp");
It is literally adding the string nbsp in, how do I make it so it actually counts as a no break space?
To replace literally all "whitespace" (spaces, tabs, etc), do this:
temp = temp.replaceAll("\\s", " ");
\s is the regex for "whitespace".
Unicode 'no-break space' character has code 0xA0, so, instead of an HTML entity, you could use it directly: "\u00A0".
The correct string to use is " "
so your code should be
temp = temp.replace(" ", " ");

Regex split with a delimiter that contains no others on it's side [duplicate]

This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Split a string by commas but ignore commas within double-quotes using Javascript
(17 answers)
Regex to replace a string not within quotes (single or double)
(1 answer)
Closed 9 years ago.
I'm stuck with this problem with regular expression.
Suppose i have a string that i've read from a file that contains.
first_name, Hello, "test Drive"
then i'll just split it using , as delimiter. and i'll get
myString[0] = "first_name";
myString[1] = "Hello";
myString[2] = "\"test Drive\"";
My problem is when the system read a string with a , inside the double quotes
first_name, Hello, "test, Drive"
i get
myString[0] = "first_name";
myString[1] = "Hello";
myString[2] = "\"test"
myString[3] = "Drive\"";
My Question
How would i split a string using , as delimiter with a condition that no " are present on it's left and right side.. or is there some workaround that will be much easier?
Thanks.
It looks like you're working on an CSV-file. Have you already considered to use one of the CSV-libraries to do this (like opencsv or supercsv)?
if you know the definite regex limit until then where you want to split, then below will work for you
String test = "first_name, Hello, \"test, Drive\"";
String[] tests = test.split(", ", 3);
System.out.println("1 " + tests[0]);
System.out.println("1 " + tests[1]);
System.out.println("1 " + tests[2]);
output :-
1 first_name
2 Hello
3 "test,Drive"
if you dont know the limit then input string should be in below format where if you can format the string within qutoes which has only (,) without space.. where other text delimiters with one comma (,) and space ( )
String test = "first_name, Hello, \"test,Drive\"";
String[] tests = test.split(", ");
System.out.println("1 " + tests[0]);
System.out.println("1 " + tests[1]);
System.out.println("1 " + tests[2])
output :-
1 first_name
2 Hello
3 "test,Drive"

Categories