0

I have the pattern

\\bi\\b[^.!?]{0,10}\\bhate

which matches string only in one sentence (not divided by .!? ). But it also matches the opposite sense with not between two words. How to exclude such occurences of not in between two words with maximum distance of 10 between them.

Now it matches: i do not hate. I would like to exclude that and leave only matches such as i do hate or i hate.

SDJ
  • 4,083
  • 1
  • 17
  • 35

1 Answers1

1

You would exclude not in a negative assertion
tailored to the length of NOT within the first 10 characters.
I.e. the range is 10 - length('not') or {0,7}

\bi\b(?!.{0,7}not)[^.!?]{0,10}\bhate

https://regex101.com/r/vdqBQX/1

 \b i \b                       # 'i'
 (?! .{0,7} not )              # Here, exclude 'not' if within the first 7 characters     
 [^.!?]{0,10} \b               # O - 10 characters within this negated class
 hate
  • thanx for such details, but length of 3 digits isn't so much matter here for me. Just needed to exclude negation – Sergey Aleshkin Alyoshkin Mar 21 '19 at 07:45
  • @SergeyAleshkinAlyoshkin - Did I make too big of a deal about the length of `'not'` ? Sorry, but you made a big deal of the length between words `between two words with maximum distance 10 between them`. Maybe you don't care so much about the _length_ between words. I guess you could just do `^i (?!.*not).*hate$` –  Mar 23 '19 at 19:37