1

I am trying to use regex to match strings that not end with 's, I wrote a regex like this: .*[^('s)$]$, but it seems that this regex also cannot match a string that ends with a single s, like cats or dogs, can somebody help? thx

I am using egrep, I have tried like

^(?!.*'s$).*

or

.*(?<!ab)$

but it seems these not working well

anubhava
  • 761,203
  • 64
  • 569
  • 643
Jasper Zhou
  • 449
  • 1
  • 5
  • 13

1 Answers1

1

Look-around assertions are traditionally not supported in grep. You may be able to usegrep -v` here:

grep -v "'s$" file

-v option is used for inver-match which is used to return lines are those not matching any of the specified patterns.


However do note in in gnu-grep you can use experimental -P option to use advanced PCRE features such as lookahead and lookbehind assertions.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • thx in advance, and now I have another question: what if I want to match a string contains a specific string like 'aa' but not end with 's ? It seems I cannot solve this problem with -v :( – Jasper Zhou Jun 04 '19 at 14:34
  • You can use pipeline so `grep 'aa' file | grep -v "'s$"` – anubhava Jun 04 '19 at 14:40