How to match the first occurrence of a pattern (or character) except for lines where that pattern is at the beginning of the line?

AlexVillarra

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.

virolino

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the first occurrence of a line containing a pattern?

How to match the last occurrence of a pattern using regex

Match a pattern in line not followed by any character

sub except the first pattern that match

how to pattern match first character in string

how to match a pattern and add a character to it

Match the first occurrence of an optional pattern

How can I match only the first N instances of a pattern, then print lines following each pattern until a blank line?

how to split line at first period character based on word count and reiterate the process in resulting lines (in the pattern space)

Sed: delete lines without pattern except first

How to match the first occurrence of a pattern (IP addr) on each line of the file?

Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed

Move pattern to beginning of line

How to make regex match pattern from the beginning?

Remove spaces except lines beginning with a specific pattern

Text Processing - How to get pattern A matching line until first occurrence of pattern B matching line?

Remove lines if pattern match except if line contains string

Using sed insert a character in the beginning of the line except line with pattern matched and empty line

How to use grep to match lines where the first character falls in a range?

Awk to print lines between first pattern and second pattern where second pattern occurs above and below the first pattern

How to match until first occurrence of a pattern?

get first occurrence of pattern

Lines from first match (pattern 1) to last match (pattern 2)

Using grep, how to match beginning of line with pattern from stdin

Match pattern on new lines after first pattern is found?

How to get the first integer from all lines that match a pattern?

How to include the first character in a RegEx match from the RegEx pattern?

sed: How to remove the first blank line after a pattern match

Delete lines below, excluding the line with match pattern