我有以下两个 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"/>
谁能帮我?
答案1
- Ctrl+H
- 找什么:
(<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
- Replace all
解释:
( # 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.
注意:如果需要,请添加其他组。
截图(之前):
截图(之后):