0

I have the following piece of code that splits the string and returns an array of strings.

public static void main(String[] args) {
      String name="what is going on";
      String[] ary = name.split("");
      System.out.println(Arrays.toString(ary));
       }
//output: [, w, h, a, t,  , i, s,  , g, o, i, n, g,  , o, n]  

To prevent the trailing spaces, the following regex was employed during split. but I would like to know how it works

public static void main(String[] args) {
          String name="what is going on";
          String[] ary = name.split("(?!^)");
          System.out.println(Arrays.toString(ary));
           } //[w, h, a, t,  , i, s,  , g, o, i, n, g,  , o, n]

if someone can explain what the regex looks for and how that regex is used for split, it will be very helpful for Java beginner community. Thanks a lot

3

2 Answers 2

4

In your first example, the empty pattern matches before every character in the string. So it matches before the first character, before the second, etc. The String.split(String) Javadoc indicates that trailing empty strings are ignored, but the returned strings includes what is before the first match. So, the array is {"", "w", "h", ..., "n"}.

The second example has a regexp that matches any place except for the beginning of the string. The (? and ) bound a lookahead. The ! makes it a negative lookahead and the ^ means the beginning of the string. Moreover, no characters are actually consumed by the regexp. So, it matches after the first character, after the second, and so on. None of the characters themselves get consumed, so you have:

 w h a t   i s   g o   i n g   o n
  ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

The carets here are break points with a space above.

Sign up to request clarification or add additional context in comments.

1 Comment

Calling the lookahead a non-capturing group might be a bit confusing, I think. Otherwise, spot-on.
1

It splits the string to substrings and divide it on the regex char or string: BUT not puts the regex into output so:

string s1 = "divided by spaces"; and s1.split("\s")[0] will be the divided s1.split("\s")[1] will be the by and NOT the " "

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.