假設(shè)我有一個(gè)文本文件,其內(nèi)容類(lèi)似于以下內(nèi)容:021 Line one of section A.021 Line two of Section A.021 Line three of section A.021 Part two of Line three of Section A.021 We just skipped line four, but that's okay.021 Back to line six.Non-formatted lines to be ignored. This can be from 0 lines, to any number of lines, and the content can be any text.033 Line 1 of Section B033 Line 2 of Section B033 Okay, that's enough.是否可以在正則表達(dá)式中給我兩組,第一組包含所有以開(kāi)頭的行021,第二組包含所有以開(kāi)頭的行033?行標(biāo)記會(huì)有所不同,但始終是\d{3}.
1 回答

婷婷同學(xué)_
TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可能會(huì)在捕獲組的開(kāi)頭捕獲數(shù)字,并\1
在重復(fù)時(shí)使用對(duì)該組的反向引用。
這將為您提供開(kāi)頭數(shù)字相同的匹配項(xiàng)。
^(\d{3}) .*(?:\r?\n\1.*)*
添加回答
舉報(bào)
0/150
提交
取消