lookarounds
printf 'abc123\n' | grep -P 'abc(?=123)'
Output:
abc123
Lookahead
(?=...) asserts what follows
(?!...) asserts what does not follow
The match does not include the assertion
Lookbehind
(?<=...) asserts what precedes
(?<!...) asserts what does not precede
Variable width lookbehind is not supported everywhere
Engines
Lookarounds are a PCRE feature POSIX ERE does not support them Do not use them in a portable script unless you control the tool
When to use
- validation without consuming the delimiter
- matching a value before a specific context
- avoiding a capture when the boundary is enough
Next
Engines in 07 engines
Posted