嗨,我在文本中寻找 notepad++ 正则表达式模式匹配,并进行一些替换。但到目前为止,我找到的方法似乎不起作用。
示例文本:
1 blablablabla. blablnsnsnns, blalblblbl: blablaa; balal blala. 2 blblb'blbµµ77777µµlblb blblb, blslsµµ105µµnlsllsl.
3 blalblblbl: blablaa; balal blala. 4 blblb'blbµµ9999µµlblb . Blblb, blslsnlsllsl. 5 jsjjsjj; gggbqbqbq: ghshhqhhqh !. Gsgsjjsskksk. 6 fshhhshs, nnsnnsns! nsnnsn. 7 blalallallal7600hhzhz ; nmmkzjzbzbzb34fspmmm :
blslslslsavccacac,
hkkdlfmfmmf56balalala.
hdfmmfm87kdkkkkfkf.
8 blalalallajhshduie.
9 bslslslslls :
blslsllsllls,
bslslllsllsls.
nsnsnnsnsnnsnns,
hsbbbslslsllsllsls.
10 bslsllsllsllsllslls à sbsbbsbbsb , snsnnsnnsnnsn.
模式搜索:
我正在寻找任意数字的开头,后跟一个空格和一些文本,并在遇到下一个以空格开头的数字之前停止。
分组应该像这样:
- 第 1 组:一个数字
- 第 2 组:数字后面的空格字符
- 组 3:一些文本也可以包含一些数字,但这些数字不在空格字符之间,而是在(µµ)之间。
预期成绩:
<VERSETAG=1>blablablabla. blablnsnsnns, blalblblbl: blablaa; balal blala.</VERSETAG>
<VERSETAG=2> blblb'blbµµ77777µµlblb blblb, blslsµµ105µµnlsllsl.</VERSETAG>
<VERSETAG=3>blalblblbl: blablaa; balal blala.</VERSETAG>
<VERSETAG=4>blblb'blbµµ9999µµlblb . Blblb, blslsnlsllsl.</VERSETAG>
<VERSETAG=5>jsjjsjj; gggbqbqbq: ghshhqhhqh !. Gsgsjjsskksk.</VERSETAG>
<VERSETAG=6>fshhhshs, nnsnnsns! nsnnsn.</VERSETAG>
<VERSETAG=7>blalallallal7600hhzhz ; nmmkzjzbzbzb34fspmmm :
blslslslsavccacac,
hkkdlfmfmmf56balalala.
hdfmmfm87kdkkkkfkf.</VERSETAG>
<VERSETAG=8>blalalallajhshduie.</VERSETAG>
<VERSETAG=9>bslslslslls :</VERSETAG>
<VERSETAG=10>bslsllsllsllsllslls à sbsbbsbbsb , snsnnsnnsnnsn.</VERSETAG>
感谢@Toto 使用此正则表达式进行测试(但到目前为止没有按预期工作,请参阅下面的结果):
Look for :
(?:^\D*|\G )(\d+)\s+(.+?)\R?(?=\s\d+\s|\z)
replace with :
<VERSETAG=$1>$2</VERSETAG>\n
测试结果:
<VERSETAG=1>blablablabla. blablnsnsnns, blalblblbl: blablaa; balal blala.</VERSETAG>
2 blblb'blbµµ77777µµlblb blblb, blslsµµ105µµnlsllsl.
<VERSETAG=3>blalblblbl: blablaa; balal blala.</VERSETAG>
<VERSETAG=4>blblb'blbµµ9999µµlblb . Blblb, blslsnlsllsl.</VERSETAG>
<VERSETAG=5>jsjjsjj; gggbqbqbq: ghshhqhhqh !. Gsgsjjsskksk.</VERSETAG>
<VERSETAG=6>fshhhshs, nnsnnsns! nsnnsn.</VERSETAG>
7 blalallallal7600hhzhz ; nmmkzjzbzbzb34fspmmm :
blslslslsavccacac,
hkkdlfmfmmf56balalala.
hdfmmfm87kdkkkkfkf.
8 blalalallajhshduie.
9 bslslslslls :
<VERSETAG=10>bslsllsllsllsllslls à sbsbbsbbsb , snsnnsnnsnnsn.</VERSETAG>
提前非常感谢您!
答案1
根据评论更新:
- Ctrl+H
- 找什么:
(?:^\D*|\G )(\d+)\s+(.+?)\R?(?=\s\d+\s|\z)
- 用。。。来代替:
<VERSETAG=$1>$2</VERSETAG>\n
- 检查环绕
- 检查正则表达式
- 查看
. matches newline
- Replace all
解释:
(?:^\D*|\G ) # non capture group, beginning of line followed by 0 or more non digits or restart from the last match position
(\d+) # group 1, 1 or more digits
\s+ # 1 or more spaces
(.+?) # group 2, 1 or more any character including new line, not greedy
\R? # any kind of linebreak, optional
(?=\s\d+\s|\z) # positive lookahead, make sure we have after 1 or more digits surround with spaces or end of file
给定示例的结果:
<VERSETAG=1>blablablabla. blablnsnsnns, blalblblbl: blablaa; balal blala.</VERSETAG>
<VERSETAG=2>blblb'blbµµ77777µµlblb blblb, blslsµµ105µµnlsllsl.</VERSETAG>
<VERSETAG=3>blalblblbl: blablaa; balal blala.</VERSETAG>
<VERSETAG=4>blblb'blbµµ9999µµlblb . Blblb, blslsnlsllsl.</VERSETAG>
<VERSETAG=5>jsjjsjj; gggbqbqbq: ghshhqhhqh !. Gsgsjjsskksk.</VERSETAG>
<VERSETAG=6>fshhhshs, nnsnnsns! nsnnsn.</VERSETAG>
<VERSETAG=7>blalallallal7600hhzhz ; nmmkzjzbzbzb34fspmmm:
blslslslsavccacac,
hkkdlfmfmmf56balalala.
hdfmmfm87kdkkkkfkf.</VERSETAG>
<VERSETAG=8>blalalallajhshduie.</VERSETAG>
截屏