后视模式 [英] Pattern in lookbehind
问题描述
我的问题与lookbehinds有关,我想找到单词this"后的所有第一个数字,我有以下数据:
My question is related with lookbehinds, I want to find all the first numbers after the word "this", I have the following data:
188282 这是一个数字 12345 和 54321
188282 这是一个数字 1234556
这是一个数字 1234556
187293 这是数字 74893 和 83978 的另一个例子
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
模式:
这是一个数字 \d+
输出:
188282 这是一个数字 12345
和 54321
188282 这是一个数字 1234556 的例子
这是一个数字 1234556 的例子
187293 这是数字 74893 和 83978 的另一个例子
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
为了匹配所有这些,我使用了一种更通用的方法,因为我知道我想要单词this"之后的第一个数字
To match all of them I used a more generic approach as I know I want the first number after the word "this"
模式:
this[^\d]+\d+
输出:
188282 这是一个数字 12345
和 54321
188282 这是一个数字 1234556 的例子
这是一个数字 1234556 的例子
187293 这是另一个数字 74893
和 83978
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
我现在想使用lookbehinds,因为我不想在结果中包含部分模式.按照我的第一种方法:
Im tring to use lookbehinds now, as I don’t want to include part of the pattern in the results. Following my first approach:
模式:
(?<=this is an example of a number )\d+
输出:
188282 这是一个数字 12345
和 54321
188282 这是一个数字示例 1234556
这是一个数字的例子1234556
187293 这是数字 74893 和 83978 的另一个例子
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.
模式:
(?<=this[^\d]+)\d+
输出:
188282 这是一个数字 12345 和 54321
188282 这是一个数字 1234556
这是一个数字 1234556
187293 这是数字 74893 和 83978 的另一个例子
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
不匹配任何内容
是否有可能在lookbehinds中有模式?我对这个问题尝试了错误的方法吗?它有点长,但我想向您展示我到目前为止所做的尝试,而不仅仅是问这个问题
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
提前致谢
推荐答案
lookbehinds 的问题是并非所有语言都支持可变宽度的lookbehinds(它们不能支持lookbehinds 里面的内容可以是可变数量的字符).
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+))
或者可能是重置匹配的 \K
正则表达式字符(如果您的正则表达式引擎支持它).
Or maybe the \K
regex character which resets a match (if your regex engine supports it).
this[^\d]+\K\d+
这篇关于后视模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!