全文=
"
......
A=
B= 12345
....."我想在A=和换行之间得到空单词"“。并希望在B=和换行之间得到"12345“。
如何使用正则表达式获取单词?
(?<=A=)\s*(\S*)\s* 或
(?<=B=)\s*(\S*)\s* 但是,它也带来了下一行内容。
发布于 2019-05-08 05:54:19
这个模式怎么样:
(?<=[A-Z]=)[ ]*(\S*)这种模式通过只允许A= (或B=等)之后的空格,避免了将其包装到下一行的问题。这意味着,在A=行的情况下,在它之后只有一个换行符,[ ]*将匹配0次。第二,对于内容,它只使用(\S*),它也不会占用空白并将其包装到下一行。
发布于 2019-05-08 06:11:24
这句话可能会这样做,如果有必要,我们当然可以添加更多的边界:
^([A-B=]+\s)([0-9]+|)我们有两个捕获组,我们可以使用$1和$2简单地调用它们。
图表
这个图表显示了表达式的工作方式,我们可以在这个链接中可视化其他表达式。

编辑:
然后,这句话可以通过创建三个捕获组来帮助我们做到这一点:
^([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}`);
});
}
RegEx 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}`);
});
}
发布于 2019-05-08 06:29:32
另一种选择是使用捕获组,而不是使用积极的查找:
^[A-Z]+[ ]*=[ ]*(\S*)^开始[A-Z]+匹配1+乘以A[ ]*=匹配0+时,空格后面跟着=[ ]*=匹配0+乘以一个空格(\S)匹配的组中捕获非空格字符(这将包含您的值)https://stackoverflow.com/questions/56034285
复制相似问题