I have a string that i am trying to extract patterns from, the string is as follows:
( ELT2N ( ELTOK wpSA910 wpSA909 wpSA908 wpSA474 ) )
The problem is, i dont know how many of the strings beginning with ‘wp’ will be in the string i am trying to search, however i want toi extract all of them using one statement. I am currently using the pattern below:
private final static String STARS_LINE_PATTERN = "\\(\\s+?(\\w+?)\\s+?\\(\\s+(\\w+)\\s+?(\\w+?\\s??){1,}\\s+?\\)\\s+?\\)";
The pattern is matching the string and returning the ‘ELT2N’ and the ‘ELTOK’ strings but is not returning the strings prefixed by ‘wp’.
Can anyone help?
Thanks
Simon
Java regex like most flavors can only keep the last capture when you repeat a capturing group.
For this particular problem, you may want to match the entire
wpsequence into one group in one regex, and then post-process it again with another regex. In this case, a simplesplitis enough.Here’s a snippet to illustrate the idea:
The above prints:
So the entire
wpsequence is captured by\3of the regex pattern, which is thensplitinto its parts.References
Related questions
Captures, but not so in Java