java string split regular expression - java

I have a string (1, 2, 3, 4), and I want to parse the integers into an array.
I can use split(",\\s") to split all but the beginning and ending elements. My question is how can I modify it so the beginning and ending parenthesis will be ignored?

You'd be better served by matching the numbers instead of matching the space between them. Use
final Matcher m = Pattern.compile("\\d+").matcher("(1, 2, 3, 4)");
while (m.find()) System.out.println(Integer.parseInt(m.group()));

Use 2 regexes: first that removes parenthesis, second that splits:
Pattern p = Pattern.compile("\\((.*)\\)");
Matcher m = p.matcher(str);
if (m.find()) {
String[] elements = m.group(1).split("\\s*,\\s*");
}
And pay attention on my modification of your split regex. It is much more flexible and safer.

You could use substring() and then split(",")
String s = "(1,2,3,4)";
String s1 = s.substring(1, s.length()-2);//index should be 1 to length-2
System.out.println(s1);
String[] ss = s1.split(",");
for(String t : ss){
System.out.println(t);
}

Change it to use split("[^(),\\s]") instead.

Related

How can I divide a string with split() into separate strings with 15 items at each index?

Normally with split() it will divide a whole string by one regex.
So if I take String objects= "door,cat,house,trash";
and do objects.split(",") it will split it into an array of String[] objects= {"door","cat","house","trash"}; as you probably know.
But I don't want to separate it with every comma. I want to separate it with every 15th item. So my list would have String[] objects= {"door,cat,house,trash...obj15","obj1,obj2,obj3...obj15"};
Does that make sense? How would I go about doing that?
split() is not good for that. Use a find() loop instead.
Demo (Java 4+)
String input = "door,cat,house,trash,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14,o15,p16,q17,r18,s19,t20";
Pattern p = Pattern.compile("(?=.)((?:[^,]*,){0,4}[^,]*),?");
List<String> result = new ArrayList<>();
for (Matcher m = p.matcher(input); m.find(); )
result.add(m.group(1));
for (String s : result)
System.out.println('"' + s + '"');
Demo (Java 9+)
String input = "door,cat,house,trash,e5,f6,g7,h8,i9,j10,k11,l12,m13,n14,o15,p16,q17,r18,s19,t20";
String[] result = Pattern.compile("(?=.)((?:[^,]*,){0,4}[^,]*),?").matcher(input)
.results().map(m -> m.group(1)).toArray(String[]::new);
Arrays.stream(result).forEach(s -> System.out.println('"' + s + '"'));
Output
"door,cat,house,trash,e5"
"f6,g7,h8,i9,j10"
"k11,l12,m13,n14,o15"
"p16,q17,r18,s19,t20"
You should of course change {0,4} to {0,14} if you want 15 values per block.
The other answers suggest using complicated regular expressions. I would avoid that. Use Guava to partition the split string into groups of fifteen, and join each group back up with commas:
String[] objects = "door,cat,trash,house,...";
List<String> list = Lists.newArrayList(objects.split(","));
String[] result = Lists.partition(list, 15).stream()
.map(each -> String.join(",", each))
.toArray(String[]::new);
First thing that came to mind is try splitting them with split(",") then loop through them and add any other symbol in the 15th place other than , (e.g $).
Then you split by that (e.g split("$"))
I don't know how effective it is but here you go.
Try the following regex pattern [a-z]+(,[a-z]+){14}. This will match
a list of 15 comma separated words. If I were you, I would switch to using the provided Matcher and Pattern classes to find this regex pattern (instead of using split).
Pattern pattern = Pattern.compile("[a-z]+(,[a-z]+){14}");
Matcher matcher = pattern.matcher("dog,door,cat,other,etc...");
while (matcher.find()) {
System.out.println(matcher.group());
}

separate mathematical expression in java

Would anyone be able to help me with separating this mathematical expression 125*1*4*4+82*1*10+2*59+2+4 in Java. I want to get the numbers form the expression, and I'm not sure how to use split() method over here.
You can use split with this regex [*+] :
String[] numbers = "125*1*4*4+82*1*10+2*59+2+4".split("[*+]");
Outputs
[125, 1, 4, 4, 82, 1, 10, 2, 59, 2, 4]
In case the mathematical expression contains spaces, you can remove them before and use split so you can use :
String[] numbers = "125*1*4*4+82*1*10+2*59+2+4".replaceAll("\\s+", "").split("[*+]");
//---------------------------------------------^----------------------^
Note you can add another arithmetic operators like [*+-/]
Another solution from eparvan:
In case you are not sure what the expression can contain you can use :
String[] numbers = "125*1*4*4+82*1*10+2*59+2+4".replaceAll("\\s+", "").split("[^0-9]");
//----------------------------------------------------------------------------^----^
Edit
What if I want to get the "+" and "*" ?
Input: 125*1*4*4+82*1*10+2*59+2+4 Output: ***+**+*++
In this case you can split with \d+ like this :
String[] numbers = "125*1*4*4+82*1*10+2*59+2+4".replaceAll("\\s+", "").split("\\d+");
But i will prefert to go with Pattern it is more practice then split for example you can use :
String str = "125*1/4*4+82*1*10+2/59-2+4";
String regex = "[^\\d]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}

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 ][

How to split a string based on a pattern using regex

I have trouble splitting string based on regex.
String str = "1=(1-2,3-4),2=2,3=3,4=4";
Pattern commaPattern = Pattern.compile("\\([0-9-]+,[0-9-]+\\)|(,)") ;
String[] arr = commaPattern.split(str);
for (String s : arr)
{
System.out.println(s);
}
Expected output,
1=(1-2,3-4)
2=2
3=3
4=4
Actual output,
1=
2=2
3=3
4=4
This regex would split as required
,(?![^()]*\\))
------------
|->split with , only if it is not within ()
This isn't well suited for a split(...). Consider scanning through the input and matching instead:
String str = "1=(1-2,3-4),2=2,3=3,4=4";
Matcher m = Pattern.compile("(\\d+)=(\\d+|\\([^)]*\\))").matcher(str);
while(m.find()) {
String key = m.group(1);
String value = m.group(2);
System.out.printf("key=%s, value=%s\n", key, value);
}
which would print:
key=1, value=(1-2,3-4)
key=2, value=2
key=3, value=3
key=4, value=4
You will have to use some look ahead mechanism here. As I see it you are trying to split it on comma that is not in parenthesis. But your regular expressions says:
Split on comma OR on comma between numbers in parenthesis
So your String gets splitted in 4 places
1) (1-2,3-4)
2-4) comma
String[] arr = commaPattern.split(str);
should be
String[] arr = str.split(commaPattern);

Categories

Resources