如何使用正则表达式查找和替换行中的所有文本,同时完整保留两个分隔符之间的字符串和空格?
How can I use regex to find and replace all text in lines while leaving a string and whitespace between two delimiters intact?
我不太擅长正则表达式...
我在 XML 文档中有数千行是这样的(空格是故意的,因为它们在 XML 中):
<col id="7" type="TEXT"> Speaker</col>
<col id="7" type="TEXT"> Monitor</col>
<col id="7" type="TEXT"> TV</col>
<col id="7" type="TEXT"> Amp</col>
并且基本上想做与此相反的事情:
[^>]+(?![^<]*>)
我想进行替换,使第一个“>”和第二个“<”之间的所有内容保持原样。
然后包装该字符串(和空格)以结束,例如:
<outline text=" Speaker"></outline>
<outline text=" Monitor"></outline>
等等。
这可能吗?
在 Atom 中,您可以简单地搜索 <[^>]*>([^<]*)<[^>]*>
并替换为 <outline text=""></outline>
。
参见regex demo。
详情
<[^>]*>
- <
,除 >
之外的 0 个或更多字符,然后是 >
([^<]*)
- Capturing 第 1 组:除 >
之外的任意 0 个或多个字符,然后是 >
<[^>]*>
- <
,除 >
之外的 0 个或更多字符,然后是 >
替换模式中的
是对存储在组 1 中的值的反向引用。
我不太擅长正则表达式...
我在 XML 文档中有数千行是这样的(空格是故意的,因为它们在 XML 中):
<col id="7" type="TEXT"> Speaker</col>
<col id="7" type="TEXT"> Monitor</col>
<col id="7" type="TEXT"> TV</col>
<col id="7" type="TEXT"> Amp</col>
并且基本上想做与此相反的事情: [^>]+(?![^<]*>)
我想进行替换,使第一个“>”和第二个“<”之间的所有内容保持原样。
然后包装该字符串(和空格)以结束,例如:
<outline text=" Speaker"></outline>
<outline text=" Monitor"></outline>
等等。
这可能吗?
在 Atom 中,您可以简单地搜索 <[^>]*>([^<]*)<[^>]*>
并替换为 <outline text=""></outline>
。
参见regex demo。
详情
<[^>]*>
-<
,除>
之外的 0 个或更多字符,然后是>
([^<]*)
- Capturing 第 1 组:除>
之外的任意 0 个或多个字符,然后是>
<[^>]*>
-<
,除>
之外的 0 个或更多字符,然后是>
替换模式中的 是对存储在组 1 中的值的反向引用。