Pattern in lookbehind

一笑奈何 提交于 2019-12-20 03:23:10

问题


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


回答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




回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/20994152/pattern-in-lookbehind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!