[英]Regex Notepad++ html search replace
我正在尝试使用记事本++中的REGEX批处理(搜索和替换)数十万个html页面。 所有的html页面具有完全相同的布局,我基本上是试图将一个元素(标题)复制到页面标记中,而该标记当前不为空
<html>
<head>
<title>some title</title>
<lots of junk and newlines>
</head>
<body>
<lots of stuff, tags, content><span>stuff</span><div>more stuff</div>
<div id="uniqueID">
<span>The Title that should be copied into head's title tag</span>
</div>
...other stuff...</body>
我可以找到:
The title tag: <title>(.*?)</title>
And the span containing the REAL title:
(\s*<div id="uniqueID">\s*)<span>(.*)</span>(\s*</div>)
但是我似乎无法将它们放入一个表达式中(忽略它们之间的垃圾),从而无法在Notepad ++中进行搜索和替换。
在每个页面(空格,换行符)中,uniqueID div都是相同的,其中没有其他内容是跨度的内容。 标题标记显然每页只出现一次。 我只是从正则表达式开始,可能性无穷无尽。 我知道它不是解析HTML的完美选择,但在这种情况下,应该这样做。 有谁知道如何将这两个表达式打补丁在一起以忽略中间的内容?
非常感谢!
您可以在Notepad ++的“替换”对话框中使用以下命令将span
中的title
复制到title
标签...
<title>(.*)</title>(.*<div id="uniqueID">\\s*<span>([A-Za-z ']*)</span>\\s*</div>)
<title>$3</title>$2
...如果选择正则表达式并选中。 与对话框中的newlin匹配 (是的,“ newlin”而不是“ newline”-至少在我使用的计算机上是Notepad ++的版本)。 通过使用$2
和$3
您可以利用对组捕获值的反向引用。
将span
s与标题匹配的约束较少的模式可能会在以后在文件中获取span
s的风险,例如:
<html>
<head>
<title>some title</title>
<lots of junk and newlines>
</head>
<body>
<lots of stuff, tags, content><span>stuff</span><div>more stuff</div>
<div id="uniqueID">
<span>The Title that should be copied into head's title tag</span>
</div>
<div>
<span>The text that should not be copied into the head's title tag but will be</span>
</div>
...other stuff...</body>
如果要从span
s复制的标题除了大写和小写字母字符,数字,空格和撇号之外还具有其他字符,则可以根据需要添加到字符组[A-Za-z ']
(例如[A-Za-z '_]
[A-Za-z ']
[A-Za-z '_]
包括下划线)。 只需注意HTML标记字符本身-例如<
和>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.