gpt4 book ai didi

javascript - 获取正则表达式中的第六行匹配

转载 作者:行者123 更新时间:2023-11-29 19:18:46 25 4
gpt4 key购买 nike

GeraLinha('1', '            860,96 ', 1'18/11/2010', '0237.02912.1999.0022512 ');
GeraLinha('1', ' 1.858,60 ', '17/11/2010', '0237.02911.1999.0033109 ');
GeraLinha('1', ' 397,76 ', '17/11/2010', '0237.02911.1999.0032989 ');
GeraLinha('1', ' 300,62 ', '17/11/2010', '0237.02911.1999.0032898 ');
GeraLinha('1', ' 915,95 ', '12/11/2010', '0422.02896.1999.0000277 ');
GeraLinha('1', ' 549,30 ', '18/11/2010', '0237.02912.1999.0059833 ');
GeraLinha('1', ' 861,88 ', '24/11/2010', '0237.02916.1999.0000134 ');
GeraLinha('1', ' 936,62 ', '19/11/2010', '0356.02703.1999.0072998 ');
GeraLinha('1', ' 384,15 ', '18/11/2010', '0033.02907.1999.0004992 ');
GeraLinha('1', ' 958,82 ', '17/11/2010', '0237.02911.1999.0032926 ');
GeraLinha('1', ' 766,86 ', '22/11/2010', '0341.02922.1999.0000166 ');
GeraLinha('1', ' 300,26 ', '17/11/2010', '0237.02911.1999.0033505 ');

我正在使用以下正则表达式来匹配这个组,但我只想要第六行。是否可以在正则表达式中定义“我想要第六场比赛”?如果根本不可能,是否有任何其他方法可以达到预期的结果?

http://regexr.com/3cas9

如您所见,我正在匹配所有内容。

/GeraLinha\((.*)\);/g

最佳答案

您可以匹配以 GeraLinha( 开头的 5 行,然后匹配并捕获第 6 行:

(?:GeraLinha\(.*\r?\n){5}(GeraLinha\(.*)

参见 regex demo

此处,(?:GeraLinha\(.*\r?\n){5} 子模式匹配 5 行,因为 {5} limiting quantifier .然后,我们只匹配并捕获下一行,即第6行。

var re = /(?:GeraLinha.*\r?\n){5}(GeraLinha.*)/; 
var str = 'GeraLinha(\'1\', \' 860,96 \', 1\'18/11/2010\', \'0237.02912.1999.0022512 \');\nGeraLinha(\'1\', \' 1.858,60 \', \'17/11/2010\', \'0237.02911.1999.0033109 \');\nGeraLinha(\'1\', \' 397,76 \', \'17/11/2010\', \'0237.02911.1999.0032989 \');\nGeraLinha(\'1\', \' 300,62 \', \'17/11/2010\', \'0237.02911.1999.0032898 \');\nGeraLinha(\'1\', \' 915,95 \', \'12/11/2010\', \'0422.02896.1999.0000277 \');\nGeraLinha(\'1\', \' 549,30 \', \'18/11/2010\', \'0237.02912.1999.0059833 \');\nGeraLinha(\'1\', \' 861,88 \', \'24/11/2010\', \'0237.02916.1999.0000134 \');\nGeraLinha(\'1\', \' 936,62 \', \'19/11/2010\', \'0356.02703.1999.0072998 \');\nGeraLinha(\'1\', \' 384,15 \', \'18/11/2010\', \'0033.02907.1999.0004992 \');\nGeraLinha(\'1\', \' 958,82 \', \'17/11/2010\', \'0237.02911.1999.0032926 \');\nGeraLinha(\'1\', \' 766,86 \', \'22/11/2010\', \'0341.02922.1999.0000166 \');\nGeraLinha(\'1\', \' 300,26 \', \'17/11/2010\', \'0237.02911.1999.0033505 \');';
var m = str.match(re);
if (m !== null) {
document.write(m[1]);
}

如果你可以考虑一种几乎非正则表达式的方法,你可以拆分行并只得到索引为 5 的行(我正在使用 /\r?\n+/ 正则表达式来匹配换行符支持 Unix 和 Windows 操作系统换行样式):

var str = 'GeraLinha(\'1\', \'            860,96 \', 1\'18/11/2010\', \'0237.02912.1999.0022512 \');\nGeraLinha(\'1\', \'          1.858,60 \', \'17/11/2010\', \'0237.02911.1999.0033109 \');\nGeraLinha(\'1\', \'            397,76 \', \'17/11/2010\', \'0237.02911.1999.0032989 \');\nGeraLinha(\'1\', \'            300,62 \', \'17/11/2010\', \'0237.02911.1999.0032898 \');\nGeraLinha(\'1\', \'            915,95 \', \'12/11/2010\', \'0422.02896.1999.0000277 \');\nGeraLinha(\'1\', \'            549,30 \', \'18/11/2010\', \'0237.02912.1999.0059833 \');\nGeraLinha(\'1\', \'            861,88 \', \'24/11/2010\', \'0237.02916.1999.0000134 \');\nGeraLinha(\'1\', \'            936,62 \', \'19/11/2010\', \'0356.02703.1999.0072998 \');\nGeraLinha(\'1\', \'            384,15 \', \'18/11/2010\', \'0033.02907.1999.0004992 \');\nGeraLinha(\'1\', \'            958,82 \', \'17/11/2010\', \'0237.02911.1999.0032926 \');\nGeraLinha(\'1\', \'            766,86 \', \'22/11/2010\', \'0341.02922.1999.0000166 \');\nGeraLinha(\'1\', \'            300,26 \', \'17/11/2010\', \'0237.02911.1999.0033505 \');';

var lines = str.split(/\r?\n+/);
if (lines !== null && lines.length>5) {
document.write(lines[5]);
}

关于javascript - 获取正则表达式中的第六行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com