我有以下2个html标签:
<title>The Secret Is Elsewhere</title>
<meta name="keywords" content="love, find, car, diamond"/>
随着正则表达式下面我可以更换的内容<title></title>
与标记的内容<meta name=..>
标签
。匹配换行符:
搜索: (<title>(.*?)<\/title>.*?)(<meta name="keywords" content=").*?("\/>)
替换者: \1\3\2\4
但是,替换后,我需要在`标记上的单词之间加上逗号
因此,输出应为:
<meta name="keywords" content="the, secret, is, elsewhere"/>
谁能帮我?
(<title>(\S+)(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?(\s+\S+)?</title>[\s\S]+?<meta name="keywords" content=")[^"]+
$1(?2$2)(?3,$3)(?4,$4)(?5,$5)(?6,$6)(?7,$7)(?8,$8)(?9,$9)(?10,$10)(?11,$11)(?12,$12(?13,$13)(?14,$14)
. matches newline
解释:
( # start group 1
<title> # literally, open tag
(\S+) # group 2, 1 or more non-space
(\s+\S+)? # group 3, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 4, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 5, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 6, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 7, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 8, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 9, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 10, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 11, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 12, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 13, 1 or more space followed by 1 or more non-space
(\s+\S+)? # group 14, 1 or more space followed by 1 or more non-space
</title> # end tag
[\s\S]+? # 1 or more any character, including newline
<meta name="keywords" content=" # literally
) # end group 1
[^"]+ # 1 or more any character that is not a quote
注意:这最多可使用13个字,如果您有13个以上的字,则可以根据需要添加任意数量的组
替代品:
$1 # content of group 1
(?2$2) # if group 2 exists, insert it
(?3,$3) # if group 3 exists, insert a comma then content of group 3
(?4,$4) # idem for group 4
(?5,$5) # idem for group 5
(?6,$6) # idem for group 6
(?7,$7) # idem for group 7
(?8,$8) # idem for group 8
(?9,$9) # idem for group 9
(?10,$10) # idem for group 10
(?11,$11) # idem for group 11
(?12,$12) # idem for group 12
etc.
注意:如果需要,添加其他组。
屏幕截图(之前):
屏幕截图(后):
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句