I have a hard time to understand the concepts of "lookahead" and "lookbehind". For example, there is a string "aaaaaxbbbbb". If we look at "x", does lookahead mean looking "x" towards "bbbbb" or "aaaaa"? I mean the direction.
相关问题
- Improve converting string to readable urls
- Regex to match charset
- Regex subsequence matching
- Accommodate two types of quotes in a regex
- Set together letters and numbers that are ordinal
相关文章
- Optimization techniques for backtracking regex imp
- Regex to check for new line
- Allow only 2 decimal points entry to a textbox usi
- Comparing speed of non-matching regexp
- Regular expression to get URL in string swift with
- 请问如何删除之前和之后的非字母中文单字
- Lazy (ungreedy) matching multiple groups using reg
- when [:punct:] is too much [duplicate]
If the regex is
x(?=insert_regex_here)
that is a (positive) look*ahead*, which looks ahead, or forwards, in other words towards "bbbb". It means "find an x that is followed byinsert_regex_here
".If the regex is
(?<=insert_regex_here)x
that is a (positive) look*behind*, which looks behind, or backwards, in other words towards "aaaa". It means "find an x that is preceded byinsert_regex_here
".You can also have negative lookahead
x(?!insert_regex_here)
meaning "x
not followed byinsert_regex_here
", and negative lookbehind(?<!insert_regex_here)x
, meaning "x not preceded byinsert_regex_here
".(The above
(?=
and(?<!
etc are Perl regex syntax - the syntax might be slightly different depending on your flavour of regex).I recommend you read the link that Chad gave in the comments. It has examples.