I am trying to match anything that isn't the previous match with regex.
I have the following regex:
(?<return>return)|(?<other>(?![ \n]).*?(?=(?:[\s\n]|$)))
If I use it on the following string, I get one match for the return
group
return
But on the following string I get the other
group:
return
(notice the space before the return).
The regex for other shouldn't match any spaces (or new lines) however (due to the (?![ \n])
.
So, for example let's say I have the following string: return abc 123
. Then I'd like to have 3 matches using the above regex: one in the return
group which contains "return", and two in the other
group containing "abc" and "123"
I can't seem to figure out why this is happening. Does anyone have an explanation for this? And how can I fix this?
CodePudding user response:
You might use
(?<return>return)|(?<other>\S )
It matches
(?<return>return)
- Group "return": areturn
substring|
- or(?<other>\S )
- Group "other": one or more non-whitespace chars.
If the return
can appear inside a non-whitespace chunk, you might want to exclude return
from the second part: (?<return>return)|(?<other>(?:(?!return)\S) )
. See demo.
If there must be a whitespace or start of string on the left, and a whitespace or end of string on the right, you may add whitespace boundaries:
(?<!\S)(?:(?<return>return)|(?<other>\S ))(?!\S)
See this regex demo.