Test string:
*This* is a test.[15] I would like[16] To figure _out how_ to do this.[20]
RegEx:
/[d+]|*.**|_.*_/gu
Actual Results:
Match 1: *This*
Match 2: [15]
Match 3: [16]
Match 4: _out how_
Match 5: [20]
Desired Results:
Match 1: *This*
Match 2: is a test.
Match 3: [15]
Match 4: I would like
Match 5: [16]
Match 6: To figure
Match 7: _out how_
Match 8: to do this.
Match 9: [20]
2
Answers
You may consider this approach that splits the input with capture group and uses
filter
to remove empty match from the start.RegEx Details:
(
: Start capture group*[^*]**
: Match substring wrapped in*
|
: OR_[^_]+_
: Match substring wrapped in_
|
: OR[d+]
: Match[number]
part)
: End capture groupIf you are looking for a pure regex implementation.
In your current regex, you have not included matching for the "non-special" words so they are not showing up in the match.
Only modifying the regex you already have. you would need something as follows
Here the addition of
.+?
is a non-greedy match until the regex encounters any of the characters mentioned in the positive lookahead(?=[|_|*)
which is ‘[‘ or ‘_’ or ‘*’ set based on your example.