Positive Lookahead /x(?=y)/ matches a pattern (x) on condition that it is followed by another pattern (y)
Negative Lookahead /x(?!y)/ matches a pattern (x) on condition that it is not followed by another pattern (y)
Positive Lookbehind /(?<=y)x/ matches a pattern (x) on condition that it is preceded by another pattern (y)
Negative Lookahead /(?<!y)x/ matches a pattern (x) on condition that it is not preceded by another pattern (y)
A key point in lookaround is the 2nd pattern (y), that is inside (?...), is never matched by the regex engine. In other words, lookarounds won't behave like captured groups with $1, $2, etc.
Get all the numbers
Rooms, bedsits, sharers £118.89 / $100.75, £84.79 / $75.90 Studios £187.50 / 150 USD, £123.80 / 100 USD 1 bedroom house/flat £265.12 / 250.35 USD, £160.36 / 140.50 USD 2 bedroom house/flat £375.70 £212.26 / 200 USD
match | position |
---|---|
118.89 | 25 |
100.75 | 35 |
84.79 | 45 |
75.90 | 54 |
187.50 | 69 |
150 | 78 |
123.80 | 89 |
100 | 98 |
1 | 106 |
265.12 | 128 |
250.35 | 137 |
160.36 | 150 |
140.50 | 159 |
2 | 170 |
375.70 | 192 |
212.26 | 200 |
200 | 209 |
Get all the £ price values excluding the £ symbol.
Rooms, bedsits, sharers £118.89 / $100.75, £84.79 / $75.90 Studios £187.50 / 150 USD, £123.80 / 100 USD 1 bedroom house/flat £265.12 / 250.35 USD, £160.36 / 140.50 USD 2 bedroom house/flat £375.70 £212.26 / 200 USD
match | position |
---|---|
118.89 | 25 |
84.79 | 45 |
187.50 | 69 |
123.80 | 89 |
265.12 | 128 |
160.36 | 150 |
375.70 | 192 |
212.26 | 200 |
Get all the USD price values excluding the text USD.
Rooms, bedsits, sharers £118.89 / $100.75, £84.79 / $75.90 Studios £187.50 / 150 USD, £123.80 / 100 USD 1 bedroom house/flat £265.12 / 250.35 USD, £160.36 / 140.50 USD 2 bedroom house/flat £375.70 £212.26 / 200 USD
match | position |
---|---|
150 | 78 |
100 | 98 |
250.35 | 137 |
140.50 | 159 |
200 | 209 |
Get all the price values in USD and $ excluding the text USD and the symbol $.
Rooms, bedsits, sharers £118.89 / $100.75, £84.79 / $75.90 Studios £187.50 / 150 USD, £123.80 / 100 USD 1 bedroom house/flat £265.12 / 250.35 USD, £160.36 / 140.50 USD 2 bedroom house/flat £375.70 £212.26 / 200 USD
match | position |
---|---|
100.75 | 35 |
75.90 | 54 |
150 | 78 |
100 | 98 |
250.35 | 137 |
140.50 | 159 |
200 | 209 |
Lookarounds can be nested. This means you can place a lookaround inside another lookaround. In a lookaround pattern x(?=y)
, y can be another lookaround pattern like (?<=b)a
. This can be written in a nested way as x(?=(?<=b)a)
.
Let's see this with an example. From the given list of positions, we are going to extract the male position titles only.
The pattern (?<=(?<!fe)male )\w+
gives the required output. In this pattern, the outer lookaround (?<=male )\w+
matches the titles that are followed by the word 'male'. To exclude female titles, we added an inner lookaround that matches the word 'male' that are not preceded by the string 'fe'.
List of Positions: Male Officer Female Doctor Female Officer Male Engineer Male Officer Female Engineer
match | position |
---|---|
Officer | 24 |
Engineer | 66 |
Officer | 80 |
The lookarounds can also be in succession to create a more complex pattern. Any two or more lookaround patterns such as (?=y)
and (?<=b)
, can be written in succession as (?<=b)x(?=y)
. We can get the same output for the above question by using two lookarounds in succession.
List of Positions: Male Officer Female Doctor Female Officer Male Engineer Male Officer Female Engineer
match | position |
---|---|
Officer | 24 |
Engineer | 66 |
Officer | 80 |