正则匹配并替换为重复模式的VScode
我有这个SVG,
<circle id="a/b/c" stroke="#2C2C2C" stroke-width="2" fill="#FFF" fill-rule="nonzero" cx="141" cy="21" r="9"/><text id="a/b/c-something" font-size="10" fill="#2C2C2C"><tspan x="0" y="10">a/b/c Something</tspan></text>
我想将id =“ A/B/C”替换为
id =“ ABC”
id =“ a/b/c-something”
to id =“ abc-something”
in text> text
并排除a/b/c某物
被替换。
我正在使用vscode,并且我尝试过(id =。+)\/(。+)
,但似乎不正确匹配它。我想要
I have this svg
<circle id="a/b/c" stroke="#2C2C2C" stroke-width="2" fill="#FFF" fill-rule="nonzero" cx="141" cy="21" r="9"/><text id="a/b/c-something" font-size="10" fill="#2C2C2C"><tspan x="0" y="10">a/b/c Something</tspan></text>
I want to replace id="a/b/c"
to id="a-b-c"
in the circle
and alsoid="a/b/c-something"
to id="a-b-c-something"
in text
and exclude a/b/c Something
from being replaced.
I'm using vscode, and I've tried (id=.+)\/(.+)
but it doesn't seem to match it properly. I want to
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做的一种通用方法是在第一个资格
/
char之前匹配和捕获零件,匹配此字符而无需捕获,然后匹配权利的零件以确保上下文正确,然后替换为第1组值 +新char +组2值。然后重复一般方案,
直到对文档没有更多更改为止。
就您而言,您可以使用
找到:&nbsp;
(\ sid =“ [^”/]*)/([^“]*” [^&gt;]*&gt;)
替换为:
$ 1- $ 2
请参阅 regex eggex demo 。 详细信息:
(
- 组1:\ s
-whitespaceid =“
- 固定的文本[^“]*
- 除以外的其他或更多chars”
char)
- 组1的结尾/
- a/
char(
- 组2:组2:[^“]*
- 除以外的零或更多字符“
“
- a”
char[^&gt;]*
- 除&gt;
以外的零或更多字符
&gt;
- a&gt;
char这将适用于文件搜索,并替换和内存搜索并替换功能。
如果您使用使用搜索并替换功能将正则GEGEX应用于单个文档,则可以使用基于外观的模式来实现单个通行证:
查找什么:&nbsp;
(?
用替换:
-
参见此regex degex degex demo 。 详细信息:
(?&lt; = \ sid =“ [^“]*)
\ s
-whitespaceid =“
- 固定的文本[^“]*
- 除以外的其他或更多chars”
char/
- a/
char(?= [^“^”]*” [^&gt;]*&gt;)
-一个积极的loohead,与紧随其后的位置相匹配[^“]*
- 除以外的零或更多字符“
“
- a”
char[^&gt;]*
- 除&gt;
以外的零或更多字符
&gt;
- a&gt;
char。A generic way to do this is to match and capture the part before the first qualifying
/
char, match this char without capturing, and then match the part on the right to ensure the context is correct, and then replace with Group 1 value + new char + Group 2 value.The general scheme is
Then repeat until no more changes are made to the document.
In your case, you can use
Find What:
(\sid="[^"/]*)/([^"]*"[^>]*>)
Replace With:
$1-$2
See the regex demo. Details:
(
- Group 1:\s
- a whitespaceid="
- a fixed text[^"]*
- zero or more chars other than a"
char)
- end of Group 1/
- a/
char(
- Group 2:[^"]*
- zero or more chars other than"
"
- a"
char[^>]*
- zero or more chars other than>
>
- a>
char)
- end of Group 2.This will work for both File search and replace and In-document search and replace feature.
If you are applying the regex against a single document using In-document search and replace feature, you can make use of a lookaround-based pattern to achieve the goal in a single pass:
Find What:
(?<=\sid="[^"]*)/(?=[^"]*"[^>]*>)
Replace With:
-
See this regex demo. Details:
(?<=\sid="[^"]*)
- a positive lookbehind that matches a location that is immediately preceded with\s
- a whitespaceid="
- a fixed text[^"]*
- zero or more chars other than a"
char/
- a/
char(?=[^"]*"[^>]*>)
- a positive looahead that matches a location that is immediately followed with[^"]*
- zero or more chars other than"
"
- a"
char[^>]*
- zero or more chars other than>
>
- a>
char.