用于匹配特定单词并忽略新行的正则表达式
RegEx for matching specific words and ignoring new lines
全文=
"
......
A=
B= 12345
....."
我想在 A= 和换行符之间得到空词 ""。
并希望在 B= 和换行符之间获得“12345”。
如何使用正则表达式获取单词?
(?<=A=)\s*(\S*)\s*
或
(?<=B=)\s*(\S*)\s*
但是,它也带来了下一行内容。
这个模式怎么样:
(?<=[A-Z]=)[ ]*(\S*)
此模式首先只允许 A=
(或 B=
等)后有空格,从而避免了换行到下一行的问题。这意味着在 A=
行的情况下,它后面只有一个换行符, [ ]*
将匹配零次。其次,对于内容它只使用 (\S*)
,这也不会消耗空格并换行到下一行。
This expression 可能会这样做,如果需要,我们当然可以添加更多边界:
^([A-B=]+\s)([0-9]+|)
我们有两个捕获组,我们可以简单地使用 </code> 和 <code>
来调用它们。
图表
此图显示了表达式的工作原理,我们可以在此 link:
中可视化其他表达式
编辑:
然后,this expression 可能会通过创建 3 个捕获组来帮助我们这样做:
^([A-Z]+)([=\s]+)([A-z0-9-]+)
测试 RegEx 1
const regex = /^([A-B=]+\s)([0-9]+|)/gm;
const str = `"
......
A=
B= 12345
....."`;
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}`);
});
}
正则表达式 2 测试
const regex = /^([A-Z]+)([=\s]+)([A-z0-9-]+)/gm;
const str = `ADFJE = 12313-asrn[5493]h`;
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}`);
});
}
另一种替代正向回顾的方法是使用捕获组:
^[A-Z]+[ ]*=[ ]*(\S*)
^
字符串开头
[A-Z]+
匹配 1+ 次 A-Z
[ ]*=
匹配 0+ 次 space 后跟 =
[ ]*=
匹配 0+ 次 space
(\S)
在匹配 0+ 次非白色space 字符的组中捕获(这将包含您的值)
全文=
"
......
A=
B= 12345
....."
我想在 A= 和换行符之间得到空词 ""。 并希望在 B= 和换行符之间获得“12345”。
如何使用正则表达式获取单词?
(?<=A=)\s*(\S*)\s*
或
(?<=B=)\s*(\S*)\s*
但是,它也带来了下一行内容。
这个模式怎么样:
(?<=[A-Z]=)[ ]*(\S*)
此模式首先只允许 A=
(或 B=
等)后有空格,从而避免了换行到下一行的问题。这意味着在 A=
行的情况下,它后面只有一个换行符, [ ]*
将匹配零次。其次,对于内容它只使用 (\S*)
,这也不会消耗空格并换行到下一行。
This expression 可能会这样做,如果需要,我们当然可以添加更多边界:
^([A-B=]+\s)([0-9]+|)
我们有两个捕获组,我们可以简单地使用 </code> 和 <code>
来调用它们。
图表
此图显示了表达式的工作原理,我们可以在此 link:
中可视化其他表达式编辑:
然后,this expression 可能会通过创建 3 个捕获组来帮助我们这样做:
^([A-Z]+)([=\s]+)([A-z0-9-]+)
测试 RegEx 1
const regex = /^([A-B=]+\s)([0-9]+|)/gm;
const str = `"
......
A=
B= 12345
....."`;
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}`);
});
}
正则表达式 2 测试
const regex = /^([A-Z]+)([=\s]+)([A-z0-9-]+)/gm;
const str = `ADFJE = 12313-asrn[5493]h`;
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}`);
});
}
另一种替代正向回顾的方法是使用捕获组:
^[A-Z]+[ ]*=[ ]*(\S*)
^
字符串开头[A-Z]+
匹配 1+ 次 A-Z[ ]*=
匹配 0+ 次 space 后跟=
[ ]*=
匹配 0+ 次 space(\S)
在匹配 0+ 次非白色space 字符的组中捕获(这将包含您的值)