下面我有几个示例字符串,使用正则表达式,我必须得到它的一部分,直到最后一次出现开闭括号,开闭括号中有可选值.
START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end ()END
START OF THE SHOW with some gibberish characters () also lots of non-sense things () with boring end ()END
START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end (III)END
START OF THE SHOW with some gibberish characters (1) also lots of non-sense things (2) with boring end (3)END
START OF THE SHOW with some gibberish characters (111) also lots of non-sense things (222) with boring end (333)END
我正在寻找的结果:
START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end
START OF THE SHOW with some gibberish characters () also lots of non-sense things () with boring end
START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end
START OF THE SHOW with some gibberish characters (1) also lots of non-sense things (2) with boring end
START OF THE SHOW with some gibberish characters (111) also lots of non-sense things (222) with boring end
首先,我试着像这样使用non-capture group
的概念,但我只得到匹配,直到开闭括号的第一个匹配.regexnon-capture group
^(.*?)(?:\(.*\))END$
但我从所有字符串中得到的输出只有这么多:
START OF THE SHOW with some gibberish characters
然后我试着用negative lookahead
和non-capture group
,regexnegative lookahead
^(.*?)(?:\(.*\))END$
但我得到了匹配,包括最后出现的开闭括号.
START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end ()
START OF THE SHOW with some gibberish characters () also lots of non-sense things () with boring end ()
START OF THE SHOW with some gibberish characters (I) also lots of non-sense things (II) with boring end (III)
START OF THE SHOW with some gibberish characters (1) also lots of non-sense things (2) with boring end (3)
START OF THE SHOW with some gibberish characters (111) also lots of non-sense things (222) with boring end (333)
我很确定我错过了一些小东西,但我不能自己弄清楚.
我很高兴有人能指出这一点.