Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

REGEX Positive lookbehind OR lookahead?

I want to make a regex that matches a word ONLY if there are a certain number of characters are ahead OR behind it. Doesn't matter what the characters are.

This is what i have so far. (?<=.{6})foo(?=.{6})

This seems to only match if there are 6+ characters behind "foo", but I want to make it so it will also match if there are 6 characters ahead as well. Sot it should be able to match both of the following examples:

this is a foo

foo this is

But it shouldn't match "foo"

Nvm I found out that this works: (?<=.{6})foo|foo(?=.{6}) I guess my question now is there a "cleaner" way of formatting it so I don't have to write "foo" twice?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use two techniques here:

  1. Make use of a non-capturing group with two alternatives after matching foo, one will require any six chars after foo and another will require them before foo:
foo(?:(?=.{6})|(?<=.{6}foo))

See the regex demo. Details:

  • foo - matches a whole word foo
  • (?: - start of a non-capturing group:
    • (?=.{6}) - a positive lookahead that requires any six chars other than line break chars to occur immediately to the right of the current location (i.e. right after foo)
    • | - or
    • (?<=.{6}foo) - a positive lookbehind that requires any six chars other than line break chars and then foo (we need to match it, too, as the regex index is currenely after foo) immediately to the left of the current location
  • ) - end of the group.
  1. Utilize a conditional construct and a lookbehind with an alternation:
(?<=(.{6})|)foo(?(1)|(?=.{6}))

See the regex demo. Details:

  • (?<=(.{6})|) - a positive lookbehind that matches a location immediately preceded with any six char other than line break chars, or an empty string
  • foo - a whole word foo
  • (?(1)|(?=.{6})) - a conditional construct that matches an empty string if Group 1 matched, else, the (?=.{6}) requires any six chars other than line break chars to occur immediately to the right of the foo word.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...