Reputation: 487
In the sentence "with the electric current density of 7 A the test arrangement" I need to check that within 5 words behind or after 7 A stands the word "density". So I use smth. like this [0..9]+[Blank]+A. The question is how to use the commands "look ahead" example1(?=example2) and "look behind" example1(?<=example) to check not only the next or previous word, but the next or previous 5 words? are there another possibilies to match that?
Thanks in advance
Upvotes: 1
Views: 408
Reputation: 1501
Lookaheads can be performed with (?=...)
for positive lookahead, (?!...)
for negative lookahead. Lookbehind is (?<...)
or (?!<...)
However, not all regular expression engines support variable-length lookbehinds, so you may have a problem there.
For the lookahead part, a naïve solution (not accounting for punctuation, words composed of things other than /w
, etc.) would be:
7 A(?=\s+(?:\b\w+\s+){0,4}density)
You'll then want to for EITHER the lookahead OR the lookbehind solution.
However, why not just check for: "density" then 0-4 words then 7 A OR 7 A then 0-4 words then "density" without lookaround? Is there a reason you need a zero-width assertion?
That would be something like:
(?:density(?:\s+\w+\b){0,4}\s+)(7 A)|(7 A)(?:\s+(?:\b\w+\s+){0,4}density)
Upvotes: 1