java regex \\S matched whitespace

I wrote this small snippet of code to explain how I think you’re regular expression is being matched.

I do not think that your \S is matching any whitespace.

  public static void main(String[] args)
  {
    int DEFAULT_PATTERN_FLAGS = Pattern.CASE_INSENSITIVE | Pattern.DOTALL;
    String s = "abc =123";
    Pattern p = Pattern.compile("(\\S[^=]++)\\s*+=\\s*+(\\S++)", DEFAULT_PATTERN_FLAGS);
    Matcher m = p.matcher(s);
    if (m.find())
    {
      System.out.println("found groups: " + m.groupCount());
      for (int i = 0; i <= m.groupCount(); ++i)
      {
        System.out.println(m.group(i));
      }
    }
    else
    {
      System.out.println("not found");
    }

    //Here is how I broke down the string 
    String[] sgroups = new String[]
    { "a", "bc", " ", "=", "", "123" };

    //Here is how I think it is matching the regular expression    
    String[] regexPieces = new String[]
    { "\\S", "[^=]++", "\\s*+", "=", "\\s*+", "\\S++" };
    for (int ai = 0; ai < sgroups.length; ai++)
    {
      p = Pattern.compile(regexPieces[ai], DEFAULT_PATTERN_FLAGS);
      m = p.matcher(sgroups[ai]);
      System.out.println(sgroups[ai] + ", " + regexPieces[ai]);
      if (m.find())
      {
        System.out.println("found");
      }
      else
      {
        System.out.println("not found");
      }
    }
  }

Here is the output of the above program

found groups: 2

abc =123

abc

123

a, \S

found

bc, [^=]++

found

, \s*+

found

=, =

found

, \s*+

found

123, \S++

found

Read more here: Source link