Level 2: Practical patterns · Lesson 18 of 30
Lookbehind
Lookbehind checks what comes before a match without including it. Use (?<=...) to require a prefix and (?<!...) to forbid one.
Lookbehind is the mirror image of lookahead. Instead of peeking forward, it checks the text immediately to the left of the current position. Like lookahead, it is zero-width: it never adds characters to the match, it only decides whether the match is allowed to start here.
Positive lookbehind is written (?<=...). The pattern (?<=\$)\d+ matches the digits in $45 but returns only 45. This is handy when a label or symbol tells you where the value is, but you only want the value itself. Examples include amounts after a currency sign, user names after @, or order numbers after #.
Negative lookbehind is written (?<!...). It succeeds only when the inner pattern does not end right before the current position. The pattern (?<!X-)\b[A-Z]\d{3}\b finds codes such as B123 while skipping any code written as X-D789.
Be careful with partial matches inside longer tokens. If you want numbers that are not negative, (?<!-)\d+ is not enough: in -45 the engine fails at 4, moves one step right, and happily matches 5 because the character before it is a digit, not a minus. Adding the digit to the forbidden set, as in (?<![-\d])\d+, prevents a match from starting in the middle of a number.
Support for lookbehind varies more than for any other common feature. JavaScript has supported it since ES2018 and allows variable-length content inside it. C# also allows variable length. Python requires a fixed width, so (?<=ab|c) fails there. PHP requires each alternative to be fixed, Java requires a bounded length, Ruby has limited support, and Go and Rust do not support lookbehind at all.
When you need portable code, a capture group is often the safer choice. Instead of (?<=\$)\d+, you can write \$(\d+) and read group 1 in your program. The full match includes the dollar sign, but the captured value is the same, and the pattern works in every engine.
In practice tasks the full match is what gets checked, so lookbehind is the tool that lets you exclude a prefix from the match while still requiring it.
Live tester
2 matches (JavaScript engine)
Items cost $45 and $120, not 300.
Examples
Numbers
/(?<=#)\d+/Order #881 and #902 shipped, 15 pending.
Matches: “881”, “902”
Only numbers directly after `#` match, and the hash itself is left out.
Text
/(?<=@)\w+/Ping @sara and @omar_test please.
Matches: “sara”, “omar_test”
The user names are matched without the `@` sign in front of them.
Codes
/(?<!X-)\b[A-Z]\d{3}\b/Use B123 and C456, skip X-D789.
Matches: “B123”, “C456”
Negative lookbehind rejects codes that come right after `X-`.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match the amounts that come right after `Rs ` without including the prefix.
Rs 500 paid, 200 pending, Rs 75 refunded
Task 2: Match hashtag words without the `#` symbol.
Loving #regex and #coding with friends
Task 3: Match only the positive numbers. Negative numbers must not match at all, not even partly.
Readings: 12, -7, 30, -45