1

I saw a regex expression in this other stackoverflow question but I didn't understand the meaning of each part.

String[] split = s.split("(?<=[\\S])[\\S]*\\s*");

The result of this is the Acronym of a sentence.

In order to understand a chaining regex expression should I start reading it from left to right or viceversa? How can I identify (or limit) each part?

Dharman
  • 30,962
  • 25
  • 85
  • 135
catra
  • 178
  • 2
  • 11

2 Answers2

1

(?<=[\\S]) states that the match should be preceded by \\S, that is, anything except for a space.

[\\S]* states that the regex should match zero or more non-space characters

\\s* matches zero or more spaces.

In essence, the regex finds a non-space character, and matches all non-space characters in front of it, along with the spaces after them.
The regex matches ohandas<space><space> and aramchand<space> from Mohandas Karamchand G

Thus, after using these matches to split the string, you end up with {"M", "K", "G"}

Note the two spaces that the regex matches after Mohandas, because the \\s* part matches zero or more spaces

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
-1

To clarify suspircius regular expression you may use the websites https://regexr.com/ or https://regex101.com/

Both mark parts with colors and explain what they do. But you have to replace the double backslashes by single backslashes.

Stefan
  • 1,789
  • 1
  • 11
  • 16
  • 1
    Just linking to off-site resources isn't a particularly helpful way to answer - those external sites may go offline at some point in the future and if/when they do your answer will lose any value that it has. Linking to off-site resources is fine, but your answer needs to be able to stand on its own without those links. In this case, you need to explain what the regex in question actually does. Once you've answered the question, *then* you can have the links as an "oh by the way, these tools can help you in the future" section. – JonK Feb 11 '20 at 16:46