Splitting a string on the double pipe(||) using String.split() - java

I'm trying to split the string with double pipe(||) being the delimiter.String looks something like this:
String str ="user#email1.com||user#email2.com||user#email3.com";
i'm able to split it using the StringTokeniser.The javadoc says the use of this class is discouraged and instead look at String.split as option.
StringTokenizer token = new StringTokenizer(str, "||");
The above code works fine.But not able to figure out why below string.split function not giving me expected result..
String[] strArry = str.split("\\||");
Where am i going wrong..?

String.split() uses regular expressions. You need to escape the string that you want to use as divider.
Pattern has a method to do this for you, namely Pattern.quote(String s).
String[] split = str.split(Pattern.quote("||"));

You must escape every single | like this str.split("\\|\\|")

try this bellow :
String[] strArry = str.split("\\|\\|");

You can try this too...
String[] splits = str.split("[\\|]+");
Please note that you have to escape the pipe since it has a special meaning in regular expression and the String.split() method expects a regular expression argument.

For this you can follow two different approaches you can follow whichever suites you best:
Approach 1:
By Using String SPLIT functionality
String str = "a||b||c||d";
String[] parts = str.split("\\|\\|");
This will return you an array of different values after the split:
parts[0] = "a"
parts[1] = "b"
parts[2] = "c"
parts[3] = "d"
Approach 2:
By using PATTERN and MATCHER
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String str = "a||b||c||d";
Pattern p = Pattern.compile("\\|\\|");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println("Found two consecutive pipes at index " + m.start());
}
This will give you the index positions of consecutive pipes:
parts[0] = "a"
parts[1] = "b"
parts[2] = "c"
parts[3] = "d"

Try this
String yourstring="Hello || World";
String[] storiesdetails = yourstring.split("\\|\\|");

Related

Best way to split "a{b}c{d}"

I'm struggling other than brute force method to split
String str = "a{b}c{d}"
into
String[] arr;
arr[0] = "a"
arr[1] = "{b}"
arr[2] = "c"
arr[3] = "{d}"
Wondering if there's a more efficient way other out there than using indexOf and subString
Based on your current edit it looks like you want to split on place which is either
directly before {
directly after }
In that case you can use split method which supports regex (regular expression). Regex provides lookaround mechanisms like
(?=subregex) to see if we are directly before something which can be matched by subregex
(?<=subregex) to see if we are directly after something which can be matched by subregex
Also { and } are considered regex metacharacters (we can use them like {m,n} to describe amount of repetitions like a{1,3} can match a, aa, aaa but not aaaa or more) so to make it normal literal we need to escape it like \{ and \}
Last thing you need is OR operator which is represented as |.
So your code can look like:
String str = "a{b}c{d}";
String[] arr = str.split("(?=\\{)|(?<=\\})"); // split at places before "{" OR after "}"
for (String s : arr){
System.out.println(s);
}
Output:
a
{b}
c
{d}
Demo: https://ideone.com/FdUbKs
just use the String.split() method (documentation)
arr = str.split()
You may use the String.split(String delimiter) method :
String str = "a {b} c {d}";
String[] arr = str.split(" ");
System.out.println(Arrays.toString(arr)); // [a, {b], c, {d}]
Use String.split()...
String[] arr = str.split(" ");
I don't know if it's as efficient as the previous regex solutions; I'm putting a single white space before { and after } then splitting string by " ":
String str = "a{b}c{d}";
String[] split = str.replace("{"," {").replace("}","} ").split(" ");
System.out.println(Arrays.toString(split));
Desired output:
[a, {b}, c, {d}]

Splitting expression with String#split()

I want to convert a infix operation to postfix operation. My code works when the input is given as already split expression through an array. But it doesn't when the input is given as the raw String expression.
String[] exp={"23","+","32"}//this works
String str="23 + 32";
String[]exp=str.split("//s+" );//this doesn't work
I think you have to use backslash instead of slash:
String[] exp = str.split("\\s+");
You had 2 issues I could see in your code. The first is that your second String[] has the same variable name as your first. The second is that you were using forward-slashes instead of back-slashes.
String[] exp = {"23","+","32"};
String str = "23 + 32";
String[] exp2 = str.split("\\s+"); // or " +"
System.out.println(Arrays.toString(exp));
System.out.println(Arrays.toString(exp2));
The above is working correctly for me.
I hope this helps.

How to extract integers from a complicated string?

I am having a hard time figuring with out. Say I have String like this
String s could equal
s = "{1,4,204,3}"
at another time it could equal
s = "&5,3,5,20&"
or it could equal at another time
s = "/4,2,41,23/"
Is there any way I could just extract the numbers out of this string and make a char array for example?
You can use regex for this sample:
String s = "&5,3,5,20&";
System.out.println(s.replaceAll("[^0-9,]", ""));
result:
5,3,5,20
It will replace all the non word except numbers and commas. If you want to extract all the number you can just call split method -> String [] sArray = s.split(","); and iterate to all the array to extract all the number between commas.
You can use RegEx and extract all the digits from the string.
stringWithOnlyNumbers = str.replaceAll("[^\\d,]+","");
After this you can use split() using deliminator ',' to get the numbers in an array.
I think split() with replace() must help you with that
Use regular expressions
String a = "asdf4sdf5323ki";
String regex = "([0-9]*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(a);
while (matcher.find())
{
String group = matcher.group(1);
if (group.length() > 0)
{
System.out.println(group);
}
}
from your cases, if the pattern of string is same in all cases, then something like below would work, check for any exceptions, not mentioned here :
String[] sArr= s.split(",");
sArr[0] = sArr[0].substring(1);
sArr[sArr.length()-1] =sArr[sArr.length()-1].substring(0,sArr[sArr.length()-1].length()-1);
then convert the String[] to char[] , here is an example converter method
You can use Scanner class with , delimiter
String s = "{1,4,204,3}";
Scanner in = new Scanner(s.substring(1, s.length() - 1)); // Will scan the 1,4,204,3 part
in.useDelimiter(",");
while(in.hasNextInt()){
int x = in.nextInt();
System.out.print(x + " ");
// do something with x
}
The above will print:
1 4 204 3

Parsing String with Regex in Java

I have a String which is formatted as such
[dgdds,dfse][fsefsf,sefs][fsfs,fsef]
How would I use Regex to quickly parse this to return an ArrayList with each value containing one "entry" as such?
ArrayList <String>:
0(String): [dgdds,dfse]
1(String): [fsefsf,sefs]
2(String): [fsfs,fsef]
Really stuck with this, any help would be great.
How about
String myData = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
List<String> list = new ArrayList<>(Arrays.asList(myData
.split("(?<=\\])")));
for (String s : list)
System.out.println(s);
Output:
[dgdds,dfse]
[fsefsf,sefs]
[fsfs,fsef]
This regex will use look behind mechanism to split on each place after ].
You should try this regex :
Pattern pattern = Pattern.compile("\\[\\w*,\\w*\\]");
Old, easy, awesome way :)
String s = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
String[] token = s.split("]");
for (String string : token) {
System.out.println(string + "]");
}
You can use simple \[.*?\] regex, which means: match a string starting with [, later zero or more characters (but as short as possible, not greedly, that's why the ? in .*?), ending with ].
This works, you can test it on Ideone:
List<String> result = new ArrayList<String>();
String input = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
Pattern pattern = Pattern.compile("\\[.*?\\]");
Matcher matcher = pattern.matcher(input);
while (matcher.find())
{
result.add(matcher.group());
}
System.out.println(result);
Output:
[[dgdds,dfse], [fsefsf,sefs], [fsfs,fsef]]
You may need to do it in two passes:
(1) Split out by the brackets if it's just a 1D array (not clear in the question):
String s = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
String[] sArray = s.split("\\[|\\]\\[|\\]");
(2) Split by the commas if you want to also divide, say "dgdds,dfse"
sArray[i].split(",");
We can use split(regex) function directly by escaping "]": "\\]" and then use it as the regex for pattern matching:
String str = "[dgdds,dfse][fsefsf,sefs][fsfs,fsef]";
String bal[] = str.split("\\]");
ArrayList<String>finalList = new ArrayList<>();
for(String s:bal)
{
finalList.add(s+"]");
}
System.out.println(finalList);
Split using this (?:(?<=\])|^)(?=\[) might work if there are nothing between ][

Escape comma when using String.split

I'm trying to perform some super simple parsing o log files, so I'm using String.split method like this:
String [] parts = input.split(",");
And works great for input like:
a,b,c
Or
type=simple, output=Hello, repeat=true
Just to say something.
How can I escape the comma, so it doesn't match intermediate commas?
For instance, if I want to include a comma in one of the parts:
type=simple, output=Hello, world, repeate=true
I was thinking in something like:
type=simple, output=Hello\, world, repeate=true
But I don't know how to create the split to avoid matching the comma.
I've tried:
String [] parts = input.split("[^\,],");
But, well, is not working.
You can solve it using a negative look behind.
String[] parts = str.split("(?<!\\\\), ");
Basically it says, split on each ", " that is not preceeded by a backslash.
String str = "type=simple, output=Hello\\, world, repeate=true";
String[] parts = str.split("(?<!\\\\), ");
for (String s : parts)
System.out.println(s);
Output:
type=simple
output=Hello\, world
repeate=true
(ideone.com link)
If you happen to be stuck with the non-escaped comma-separated values, you could do the following (similar) hack:
String[] parts = str.split(", (?=\\w+=)");
Which says split on each ", " which is followed by some word-characters and an =
(ideone.com link)
I'm afraid, there's no perfect solution for String.split. Using a matcher for the three parts would work. In case the number of parts is not constant, I'd recommend a loop with matcher.find. Something like this maybe
final String s = "type=simple, output=Hello, world, repeat=true";
final Pattern p = Pattern.compile("((?:[^\\\\,]|\\\\.)*)(?:,|$)");
final Matcher m = p.matcher(s);
while (m.find()) System.out.println(m.group(1));
You'll probably want to skip the spaces after the comma as well:
final Pattern p = Pattern.compile("((?:[^\\\\,]|\\\\.)*)(?:,\\s*|$)");
It's not really complicated, just note that you need four backslashes in order to match one.
Escaping works with the opposite of aioobe's answer (updated: aioobe now uses the same construct but I didn't know that when I wrote this), negative lookbehind
final String s = "type=simple, output=Hello\\, world, repeate=true";
final String[] tokens = s.split("(?<!\\\\),\\s*");
for(final String item : tokens){
System.out.println("'" + item.replace("\\,", ",") + "'");
}
Output:
'type=simple'
'output=Hello, world'
'repeate=true'
Reference:
Pattern: Special Constructs
I think
input.split("[^\\\\],");
should work. It will split at all commas that are not preceeded with a backslash.
BTW if you are working with Eclipse, I can recommend the QuickRex Plugin to test and debug Regexes.

Categories

Resources