2

My question is related with lookbehinds, I want to find all the first numbers after the word "this", I have the following data:

188282 this is an example of a number 12345 and 54321
188282 this is an example of a number 1234556
this is an example of a number 1234556
187293 this is another example of a number 74893 and 83978

Pattern:

this is an example of a number \d+

Output:

188282 this is an example of a number 12345 and 54321
188282 this is an example of a number 1234556
this is an example of a number 1234556
187293 this is another example of a number 74893 and 83978

To match all of them I used a more generic approach as I know I want the first number after the word “this”

Pattern:

this[^\d]+\d+

Output:

188282 this is an example of a number 12345 and 54321
188282 this is an example of a number 1234556
this is an example of a number 1234556
187293 this is another example of a number 74893 and 83978

Im tring to use lookbehinds now, as I don’t want to include part of the pattern in the results. Following my first approach:

Pattern:

(?<=this is an example of a number )\d+

Output:

188282 this is an example of a number 12345 and 54321
188282 this is an example of a number 1234556
this is an example of a number1234556
187293 this is another example of a number 74893 and 83978

Looks I’m getting there, I want to cover the last case as before, so I tried my second approach.

Pattern:

(?<=this[^\d]+)\d+

Output:

188282 this is an example of a number 12345 and 54321
188282 this is an example of a number 1234556
this is an example of a number 1234556
187293 this is another example of a number 74893 and 83978

Doesn’t match anything
Is it possible to have patterns inside lookbehinds? Am I trying a wrong approach to this problem? It’s a bit long but I wanted to show you what I tried so far instead of just asking the question

Thanks in advance

Joao Raposo
  • 125
  • 8

3 Answers3

2

Yes, you can use patterns inside lookbehinds, but that you can't do in most flavor of regex is to have a variable length lookbehind. In other words, you can't use a quantifier (but a fixed quantifier like {n} is allowed) inside a lookbehind. But some regex flavour allows you to use the alternation | or a limited (like in java) quantifier {1,n}.

With .net languages variable length lookbehinds are allowed.

Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113
  • This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Lookarounds". – aliteralmind Apr 10 '14 at 00:32
  • @aliteralmind: Cool, I will try to improve it as soon as possible. (I am currently editing several posts with the same mistake) – Casimir et Hippolyte Apr 10 '14 at 00:42
  • Looking forward to it. – aliteralmind Apr 10 '14 at 00:53
  • This is experimentally allowed in Perl since 5.30: https://perldoc.pl/perl5300delta#Limited-variable-length-lookbehind-in-regular-expression-pattern-matching-is-now-experimentally-supported – Grinnz Jan 21 '20 at 22:10
1

The thing with lookbehinds is that not all languages support variable width lookbehinds (they can't support lookbehinds where what's inside can be of variable number of characters).

What you can do, might be using a lookahead and a capture group:

(?=this[^\d]+(\d+))

regex101 demo

Or maybe the \K regex character which resets a match (if your regex engine supports it).

this[^\d]+\K\d+

regex101 demo

Jerry
  • 67,172
  • 12
  • 92
  • 128
  • Thanks for the alternative approaches – Joao Raposo Jan 08 '14 at 11:39
  • Funny enough .Net doesn't support \K (you mentioned in case it supports) but it does support variable width lookbehinds – Joao Raposo Jan 08 '14 at 12:04
  • @JoaoRaposo Yup! That's true. Go figure why some language implement some stuff while others don't! JavaScript doesn't support either! If your language/regex engine doesn't support either (might be rare, but who knows), I'd say simply use `this[^\d]+(\d+)` and take only the first capture group (ignoring the main capture). – Jerry Jan 08 '14 at 12:07
  • I'm a .net dev, so I guess I'll be ok regarding this issue, but i'm def going to have a look at the differences, to be honest I wasn't aware of this matter. Thanks again for the advice – Joao Raposo Jan 08 '14 at 12:11
0

It depends on your implementation of regex. You'll have to do some testing for sure.

I know that some implementations don't like this:

(?<=\d{1,5}) or (?<=\w*)

But they will work fine with this:

(?<=\d{5}) or (?<=\w{1000})

In other words, no repetition or flexible lengths.

Vasili Syrakis
  • 8,340
  • 1
  • 31
  • 51