如何从捕获的通配符中删除空格?
How to remove spaces from a captured wildcard?
我正在尝试使用正则表达式在 Notepad++ 中使用查找和替换来更改一些 XML。
这是我要捕获的特定 XML:
<category name="Content Server Categories:FOLDER:test category">
<attribute name="test attribuut"><![CDATA[test]]></attribute>
<attribute name="test attribuut1"><![CDATA[test1]]></attribute>
</category>
以下 'FIND' 正则表达式完成工作(目前):
<(category) name="Content Server Categories:(.+?)">(.+)</(category)>
现在我需要将 XML 替换为:
<category-FOLDER:testcategory name="Content Server Categories:FOLDER:test category">
<attribute name="test attribuut"><![CDATA[test]]></attribute>
<attribute name="test attribuut1"><![CDATA[test1]]></attribute>
</category-FOLDER:testcategory>
目前我尝试使用这个 'REPLACE BY' 正则表达式:
<(-) name="Content Server Categories:()">()</(-)>
但这会产生以下输出:
<category-FOLDER:test category name="Content Server Categories:FOLDER:test category">
<attribute name="test attribuut"><![CDATA[test]]></attribute>
<attribute name="test attribuut1"><![CDATA[test1]]></attribute>
</category-FOLDER:test category>
如你所见,我得到了 category-FOLDER:test category
而不是 category-FOLDER:testcategory
需要删除 space(s)..
问题是输入看起来可能不同。现在是这样的:
<category name="Content Server Categories:FOLDER:test category">
但它也可能看起来像这些示例:
<category name="Content Server Categories:FOLDER1:FOLDER2:test category">
<category name="Content Server Categories:FOLDER NAME:test category">
<category name="Content Server Categories:FOLDER NAME: FOLDER NAME1:test category">
<category name="Content Server Categories:FOLDER:test category name">
...
如何正确捕获所有这些并删除 spaces?
编辑:差点忘了,
'. Matches newline' is __ON__
一种方法可能是分两步完成,因为之后要替换多个空格。
获取需要的结构(注意使用非贪婪版本.*?
防止过度匹配):
<(category) name="Content Server Categories:(.+?)">(.+?)</(category)>
在替换中使用不带括号的替换,否则它们将包含在替换中:
<- name="Content Server Categories:"></->
然后使用 \G
:
来匹配使用重复匹配的空格
(?:</?category-|\G(?!^))\K\s*([\w:]+) (?!name=)
在替换中用捕获组 1 替换空格 </code></p>
<p><strong>说明</strong></p>
<ul>
<li><code>(?:
非捕获组
</?category-FOLDER
匹配带有可选 /
的文本
|
或
\G(?!^)
在上一场比赛结束时断言位置
)
关闭非捕获组
\K\s*
忘记之前匹配的,再匹配0+个空白字符
([\w:]+)
在组 1 中捕获匹配 1+ 次单词 char 或 :
(?!name=)
断言右边的不是不是'name='
我正在尝试使用正则表达式在 Notepad++ 中使用查找和替换来更改一些 XML。
这是我要捕获的特定 XML:
<category name="Content Server Categories:FOLDER:test category">
<attribute name="test attribuut"><![CDATA[test]]></attribute>
<attribute name="test attribuut1"><![CDATA[test1]]></attribute>
</category>
以下 'FIND' 正则表达式完成工作(目前):
<(category) name="Content Server Categories:(.+?)">(.+)</(category)>
现在我需要将 XML 替换为:
<category-FOLDER:testcategory name="Content Server Categories:FOLDER:test category">
<attribute name="test attribuut"><![CDATA[test]]></attribute>
<attribute name="test attribuut1"><![CDATA[test1]]></attribute>
</category-FOLDER:testcategory>
目前我尝试使用这个 'REPLACE BY' 正则表达式:
<(-) name="Content Server Categories:()">()</(-)>
但这会产生以下输出:
<category-FOLDER:test category name="Content Server Categories:FOLDER:test category">
<attribute name="test attribuut"><![CDATA[test]]></attribute>
<attribute name="test attribuut1"><![CDATA[test1]]></attribute>
</category-FOLDER:test category>
如你所见,我得到了 category-FOLDER:test category 而不是 category-FOLDER:testcategory
需要删除 space(s)..
问题是输入看起来可能不同。现在是这样的:
<category name="Content Server Categories:FOLDER:test category">
但它也可能看起来像这些示例:
<category name="Content Server Categories:FOLDER1:FOLDER2:test category">
<category name="Content Server Categories:FOLDER NAME:test category">
<category name="Content Server Categories:FOLDER NAME: FOLDER NAME1:test category">
<category name="Content Server Categories:FOLDER:test category name">
...
如何正确捕获所有这些并删除 spaces?
编辑:差点忘了,
'. Matches newline' is __ON__
一种方法可能是分两步完成,因为之后要替换多个空格。
获取需要的结构(注意使用非贪婪版本.*?
防止过度匹配):
<(category) name="Content Server Categories:(.+?)">(.+?)</(category)>
在替换中使用不带括号的替换,否则它们将包含在替换中:
<- name="Content Server Categories:"></->
然后使用 \G
:
(?:</?category-|\G(?!^))\K\s*([\w:]+) (?!name=)
在替换中用捕获组 1 替换空格 </code></p>
<p><strong>说明</strong></p>
<ul>
<li><code>(?:
非捕获组
</?category-FOLDER
匹配带有可选/
的文本
|
或\G(?!^)
在上一场比赛结束时断言位置
)
关闭非捕获组\K\s*
忘记之前匹配的,再匹配0+个空白字符([\w:]+)
在组 1 中捕获匹配 1+ 次单词 char 或 :(?!name=)
断言右边的不是不是'name='