I have the following example lines:
!foobar foo bar ! foo ---> NO MATCH
foo bar foo bar ! foo ---> Should match !
foo bar foo bar ! ! fooo bar ! ---> Should ONLY match the first !
! ! foo bar ---> NO MATCH
!foo bar foo bar ! ! fooo bar ! ---> NO MATCH
and I would like to find a regex expression that would match only the first occurrence of the character "!" except where the lines if the character is at the beginning of the line. I have tried to no avail using many different combinations (maybe not properly) of negative lookback like ^(?!\!)\!
.
I'm planning to use this regex expression as an input for a command that needs forcefully a regex expression, hence I can't use other methods.
Regex:
^[^!]+(!).*
Group 1 contains your match.
Test here.
Please note that this solution works only with single characters (in this case, !
).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments