Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - Pattern in lookbehind

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...