我正在尝试编写一个正则表达式来匹配某些模式,除了那些具有前面模式的模式。换句话说,给出以下句子:
Don't want to match paragraph 1.2.3.4 but this instead 5.6.7.8
我想匹配所有前面没有paragraph
字的X.X.X.X
,即它应该只匹配5.6.7.8。我当前的正则表达式似乎与 1.2.3.4 和 5.6.7.8 匹配。我已经切换了展望,但似乎与我的用例不匹配。
(?<!paragraph)(?:[()0-9a-zA-Z]+.)+[()0-9a-zA-Z]+
我用javascript编写代码。
编辑:请注意,X.X.X.X
不是固定在4X
秒。它们的范围从X.X
到X.X.X.X.X
您的模式匹配,因为"段落"与"段落[空格]"不同。您的模式没有空格。你的文字可以。
您可能希望将空格(也许是有条件的?(添加到您的后视中。因为你想要匹配不同数量的X.X.X.X
(你已经说过X.X
到X.X.X.X.X
(,所以我们还需要在后视中包含X.
:
const rex = /(?<!paragraph *(?:[()0-9a-zA-Z]+.)*)(?:[()0-9a-zA-Z]+.){1,4}[()0-9a-zA-Z]/i;
现场示例:
function test(str) {
const rex = /(?<!paragraph *(?:[()0-9a-zA-Z]+.)*)(?:[()0-9a-zA-Z]+.){1,4}[()0-9a-zA-Z]/i;
const match = rex.exec(str);
console.log(match ? match[0] : "No match");
}
console.log("Testing four 'digits':");
test("Don't want to match paragraph 1.2.3.4 but this instead 5.6.7.8 blah");
console.log("Testing two 'digits':");
test("Don't want to match paragraph 1.2.3.4 but this instead 5.6 blah");
console.log("Testing two 'digits' again:");
test("Don't want to match paragraph 1.2 but this instead 5.6 blah");
console.log("Testing five 'digits' again:");
test("Don't want to match paragraph 1.2 but this instead 5.6.7.8.9 blah");
该表达式要求:
paragraph
后跟零个或多个空格,可能后跟X.
zer 或更多次,不是在比赛之前;- 该
X.
重复一到四次({1,4}
(; - 紧随这三个之后的
X
我示例中X
A-Z0-9
,并且我已使表达式不区分大小写,但您可以根据需要进行调整。
请注意,lookback最近才在ES2018中添加到JavaScript中,因此支持需要最新的JavaScript环境。如果您需要查看旧环境,可以查看Steven Levithan出色的XRegex库。
另请注意,并非所有语言都支持像上面这样的可变长度后视(但在 JavaScript 中受支持......在最新的引擎中(。
如果您总是想匹配 4 项组,您可以这样做:
(?<!paragraph )([0-9]+.?){4}
你可以迭代地构建正则表达式 -
- 忽略前面带有单词"段落"和空格的任何单词。
- 由于您的模式是固定的,它将由由句点分隔的四倍数字组成,因此可以安全地假设该四位数中的最小位数为 1。 捕获
- 捕获组中的四倍数字以供以后使用。
在此处测试正则表达式。
const inputData = 'Don't want to match paragraph 1.2.3.4 but this instead 5.6.7.8 and 12.2.333.2';
const re = /(?<!paragraphs+)(d{1,}.d{1,}.d{1,}.d{1,})/ig;
const matchedGroups = inputData.matchAll(re);
for (const matchedGroup of matchedGroups) {
console.log(matchedGroup);
}