28

I have a string:

String str = "a + b - c * d / e < f > g >= h <= i == j";

I want to split the string on all of the operators, but include the operators in the array, so the resulting array looks like:

[a , +,  b , -,  c , *,  d , /,  e , <,  f , >,  g , >=,  h , <=,  i , ==,  j]

I've got this currently:

public static void main(String[] args) {
    String str = "a + b - c * d / e < f > g >= h <= i == j";
    String reg = "((?<=[<=|>=|==|\\+|\\*|\\-|<|>|/|=])|(?=[<=|>=|==|\\+|\\*|\\-|<|>|/|=]))";

    String[] res = str.split(reg);
    System.out.println(Arrays.toString(res));
}

This is pretty close, it gives:

[a , +,  b , -,  c , *,  d , /,  e , <,  f , >,  g , >, =,  h , <, =,  i , =, =,  j]

Is there something I can do to this to make the multiple character operators appear in the array like I want them to?

And as a secondary question that isn't nearly as important, is there a way in the regex to trim the whitespace off from around the letters?

4
  • 11
    You could just split by spaces in your example expression to get the result you want.
    – Jeffrey
    Mar 25, 2012 at 0:31
  • 1
    for your secondary question: String has a trim function: docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
    – ma cılay
    Mar 25, 2012 at 0:32
  • 3
    @Jeffrey: The spaces won't necessarily be there. I have the spaces in there for ease of readability, but it could be any combination of spaces or none. Thanks for the idea though!
    – user677786
    Mar 25, 2012 at 0:36
  • @user306848: Yeah, I know about trim, I was just curious if it was possible in the regex. Thanks for the tip though!
    – user677786
    Mar 25, 2012 at 0:37

6 Answers 6

50
String[] ops = str.split("\\s*[a-zA-Z]+\\s*");
String[] notops = str.split("\\s*[^a-zA-Z]+\\s*");
String[] res = new String[ops.length+notops.length-1];
for(int i=0; i<res.length; i++) res[i] = i%2==0 ? notops[i/2] : ops[i/2+1];

This should do it. Everything nicely stored in res.

3
  • Yeap, this works, just strip off the leading element from the array (which is empty) Mar 25, 2012 at 2:00
  • After coming back, this seems like the best way to do it. I'd like to have done it in the regex, but this will work perfectly. Thanks!
    – user677786
    Mar 25, 2012 at 5:08
  • Any explanation? Apr 2, 2019 at 12:41
20
str.split (" ") 
res27: Array[java.lang.String] = Array(a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j)
6
    String str = "a + b - c * d / e < f > g >= h <= i == j";
    String reg = "\\s*[a-zA-Z]+";

    String[] res = str.split(reg);
    for (String out : res) {
        if (!"".equals(out)) {
            System.out.print(out);
        }
    }

Output : + - * / < > >= <= ==

0
1

You could split on a word boundary with \b

2
  • Did you try it? You’re going to have a problem.
    – tchrist
    Mar 25, 2012 at 0:46
  • OK, I admit it, I tested it in .NET and it worked. Removing the empty entries should be trivial, and removing the spaces in the string is surely easily accomplished with a .replaceAll before applying the Regex. Mar 25, 2012 at 0:56
0

Can you invert your regex so split by the non operation characters?

String ops[] = string.split("[a-z]")
// ops == [+, -, *, /, <, >, >=, <=, == ]   

This obviously doesn't return the variables in the array. Maybe you can interleave two splits (one by the operators, one by the variables)

1
  • While not the exact solution, it did give me the idea that worked! Thanks! I'll edit the main post for the solution!
    – user677786
    Mar 25, 2012 at 1:21
-4

You could also do something like:

String str = "a + b - c * d / e < f > g >= h <= i == j";
String[] arr = str.split("(?<=\\G(\\w+(?!\\w+)|==|<=|>=|\\+|/|\\*|-|(<|>)(?!=)))\\s*");

It handles white spaces and words of variable length and produces the array:

[a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.