rJqnr rJqnr - 3 months ago 13
Java Question

Ignoring withspaces and comments with regex pattern in Java

I'm working on a Java program and I need help to create this pattern:

I\sam\s(.+)


I would like to ignore all the whitespaces after the first group, for example if the name is "George P " it should be counted only the "George P".
Also I wanted to consider the comment character *, ignoring all the characters after this. If there's is no * and it's found many whitespaces the matcher should fail.

sln sln
Answer

update
The asterisk is NOT included in group 1:

I\sam\s([^*]*)(?:(?<!\s)|(?<=I\sam\s))

Explained

 I \s am \s                    # 'I am '

 ( [^*]* )                     # (1), Capture after that, but not asterisk or after

 (?:                           # Trimming
      (?<! \s )                     # Trim, no wsp behind
   |                              # or,
      (?<= I \s am \s )             # Just 'I am ' behind
 )

The asterisk IS included in group 1:

I\sam\s([^*]*\*?)(?:(?<!\s)|(?<=I\sam\s))

(Note -Trimming conditions make this regex more complex. It's better to not trim within the regex, but use language api's to trim the contents of capture 1 after the match.)

Explained

 I \s am \s                  # 'I am '

 ( [^*]* \*? )               # (1), Capture after that, but not after asterisk

 (?:                         # Trimming
      (?<! \s )                   # Trim, no wsp behind
   |                            # or,
      (?<= I \s am \s )           # Just 'I am ' behind
 )