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

时间:2015-12-01 22:02:28

标签: javascript regex

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

2 个答案:

答案 0 :(得分:3)

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

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

请参阅regex demo

此处,(?:GeraLinha\(.*\r?\n){5}子模式与{5} limiting quantifier的5行匹配。然后,我们只匹配并捕获下一行是第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]);
  }




答案 1 :(得分:0)

一次只匹配所有6条线 第6行内容(使用正则表达式)位于捕获组1中。

这是有效的,因为量化组的每次迭代{6}
捕获缓冲区1被覆盖,仅留下第6行数据。

对于连续线:

text.match(/(?:^GeraLinha\((.*)\);\s*){6}/m);

对于非连续线:

text.match(/(?:^GeraLinha\((.*)\);\s*(?:(?!^GeraLinha\(.*\);\s*)^.*\s*)*){6}/m);

格式化:

 (?:
      ^ GeraLinha\(
      ( .* )                        # (1)
      \); \s* 
      (?:
           (?! ^ GeraLinha\( .* \); \s* )
           ^ .* \s* 
      )*
 ){6}