替换上下文中字符的所有实例
Replace all instances of a character within context
下面是我拥有的一个非常大的文本文件的示例:
@Don't replace this line: <http://nomatch.com#>
/or this line!/
<http://example.com#Example/Replace/With/Underscores> and <http://example.com#Example/Replace/With/Underscores2>
</Nothing to replace on this line either./>
我想在 <http://example.com#Example/
之后和 >
之前的每一行中替换所有 /
(正斜杠)实例。因此,生成的文件应如下所示:
@Don't replace this line: <http://nomatch.com#>
/or this line!/
<http://example.com#Example_Replace_With_Underscores> and <http://example.com#Example_Replace_With_Underscores>
</Nothing to replace on this line either./>
到目前为止,我已经想出了正则表达式:(?=.*?>)/
,但这是在带有 >
(link to example) 的行上抓取每个 /
。目前我正在使用 VSCode 执行查找和替换。
您可以使用
(?<=<http://example\.com#Example/[^>]*)/(?=[^>]*>)
见regex demo。 详情:
(?<=<http://example\.com#Example/[^>]*)
- 正后视使得 <http://example.com#Example/
字符串后跟任何零个或多个字符,而不是 >
尽可能多地紧接在当前字符串的左侧位置
/
- 一个 /
字符
(?=[^>]*>)
- 一个积极的前瞻(如果你需要确保有一个关闭的 >
)使得除了 >
之外还有零个或多个字符,然后是>
当前位置右边的字符。
参见Visual Studio代码演示和设置:
下面是我拥有的一个非常大的文本文件的示例:
@Don't replace this line: <http://nomatch.com#>
/or this line!/
<http://example.com#Example/Replace/With/Underscores> and <http://example.com#Example/Replace/With/Underscores2>
</Nothing to replace on this line either./>
我想在 <http://example.com#Example/
之后和 >
之前的每一行中替换所有 /
(正斜杠)实例。因此,生成的文件应如下所示:
@Don't replace this line: <http://nomatch.com#>
/or this line!/
<http://example.com#Example_Replace_With_Underscores> and <http://example.com#Example_Replace_With_Underscores>
</Nothing to replace on this line either./>
到目前为止,我已经想出了正则表达式:(?=.*?>)/
,但这是在带有 >
(link to example) 的行上抓取每个 /
。目前我正在使用 VSCode 执行查找和替换。
您可以使用
(?<=<http://example\.com#Example/[^>]*)/(?=[^>]*>)
见regex demo。 详情:
(?<=<http://example\.com#Example/[^>]*)
- 正后视使得<http://example.com#Example/
字符串后跟任何零个或多个字符,而不是>
尽可能多地紧接在当前字符串的左侧位置/
- 一个/
字符(?=[^>]*>)
- 一个积极的前瞻(如果你需要确保有一个关闭的>
)使得除了>
之外还有零个或多个字符,然后是>
当前位置右边的字符。
参见Visual Studio代码演示和设置: