Java regex后面:无效的regexp组

问题描述 投票:1回答:3

我有以下关于正则表达式/-+|(?<=: ?).*的小例子。但这会导致Node / Chrome中出现无限循环,并导致Firefox中出现“ Invalig regex group”错误。

[当我将其更改为/-+|(?<=: ).*/gm时(在后面留出了?量词),它会运行,但是-当然-在:之后我没有得到没有值的行。

如果我将正则表达式更改为/-+|(?<=:).*/gm(将空间留在后面,那么我将再次陷入无限循环/错误。

有人可以向我解释此行为,我还必须使用什么正则表达式来匹配以冒号结尾的行?我很想了解...

const text = `
-------------------------------------
Prop Name: 5048603
Prop2 Name:
Bla bla bla: asjhgg | a3857
Location: Something...
-------------------------------------
Prop Name: 5048603
Prop2 Name:
Bla bla bla: asjhgg | a3857
Location: Something...
-------------------------------------
`;

const pattern = /-+|(?<=: ?).*/gm;

let res;
while((res = pattern.exec(text)) !== null)
{
    console.log(`"${res[0]}"`);
} 

编辑:

预期输出是:

"-------------------------------------"
"5048603"
""
"asjhgg | a3857"
"Something..."
"-------------------------------------"
"5048603"
""
"asjhgg | a3857"
"Something..."
"-------------------------------------"
javascript regex regex-lookarounds
3个回答
0
投票

尝试使用此模式:/(.*):(.*)/mg

const regex = /(.*):(.*)/mg;
const str = `-------------------------------------
Prop Name: 5048603
Prop2 Name:
Bla bla bla: asjhgg | a3857
Location: Something...
-------------------------------------
Prop Name: 5048603
Prop2 Name:
Bla bla bla: asjhgg | a3857
Location: Something...
-------------------------------------`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

0
投票

(?<=...)环顾四周是积极的一面,并且在FireFox中尚不支持(请参阅supported environments here,因此,在实施之前,您总是会得到一个异常。

/-+|(?<=: ?).*模式属于可能与空字符串匹配的模式,这是一种非常典型的“病理”类型的模式。 g标志使JS正则表达式引擎匹配所有出现的模式,并执行此操作,在有效匹配时将其lastIndex前进,但是如果匹配长度为零,则不进行匹配并保持在相同的位置再次尝试相同的正则表达式,您最终陷入循环。请参阅here如何正确移动lastIndex以避免在这些情况下出现无限循环。

根据我的观察,您希望删除第一个:之前的所有行首,包括:和之后的所有空格。您可以使用

text.replace(/^[^:\r\n]+:[^\S\r\n]*/gm, '')

或者,如果您想实际提取全部为-或全部为:之后的行,则可以使用

const text = `
-------------------------------------
Prop Name: 5048603
Prop2 Name:
Bla bla bla: asjhgg | a3857
Location: Something...
-------------------------------------
Prop Name: 5048603
Prop2 Name:
Bla bla bla: asjhgg | a3857
Location: Something...
-------------------------------------
`;

const pattern = /^-+$|:[^\S\r\n]*(.*)/gm;

let res;
while((res = pattern.exec(text)) !== null)
{
    if (res[1] != undefined) {
      console.log(res[1]);
    } else {
      console.log(res[0]);
    }
}

-1
投票

正则表达式前瞻的定义是这样的(?= pattern)而不是这样的(pattern?)

https://www.regular-expressions.info/lookaround.html

© www.soinside.com 2019 - 2024. All rights reserved.