2

I am struggling with a particular regex i need to pass into the grep -E

in plain english, the regex must match the word if it matches both of these two conditions:

  • the word begins at the start of the line OR begins with a space character or any of the following "'`(,:;<[{|~

  • the word ends in "'),:;>]`}|~ or a space character, OR is at the end of the line.

####should be matched#### ignore the red text#########3
suvoo:
:suvoo:
 suvoo 
 suvoo:
suvoo'
suvoo`
`suvoo`
"suvoo"
'suvoo:
 'suvoo "ghj
'suvoo
[suvoo~
{suvoo'
his name was `suvoo'
his name is suvoo and he is 25
####shouldnt be matched####
asddsuvooaed
:suvo@abc:
suvooo
1suvoo
suvoo1:

I'm not sure about why my command does not matched anything, i might have completely misunderstood what needs escaping etc

grep -Einr "(^|[[:space:][\"'(,:;<[\`{|~]])suvoo([[:space:][]\"'),:;>\`}|~]]|$)" .
Inian
  • 80,270
  • 14
  • 142
  • 161
j58765436
  • 155
  • 1
  • 5

1 Answers1

3

You should write it as

grep -E '(^|[[:space:]"'"'"'`(,:;<[{|~])suvoo([][:space:]"'"'"'),:;>`}|~]|$)'

See the online demo

You misplaced the square brackets, note that ] inside a bracket expression should be placed at the very beginning. Also, since your quotation marks are mixed, it is safer to use a common technique to use single quotes inside single quotes.

Details

  • [[:space:]"'"'"'`(,:;<[{|~] - translates into [[:space:]"'`(,:;<[{|~] and matches whitespace, ", ', `, (, ,, :, ;, <, [, {, | or ~.
  • [][:space:]"'"'"'),:;>`}|~] translates into [][:space:]"'),:;>`}|~] and matches ], whitespace, ", ', ), ,, :, ;, >, `, }, | or ~.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563