正则匹配并替换为重复模式的VScode

发布于 2025-02-02 23:10:39 字数 556 浏览 0 评论 0原文

我有这个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 also
id="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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2025-02-09 23:10:39

这样做的一种通用方法是在第一个资格/ char之前匹配和捕获零件,匹配此字符而无需捕获,然后匹配权利的零件以确保上下文正确,然后替换为第1组值 +新char +组2值。

然后重复一般方案,

Find:    (left-hand context)text to remove/replace(right-hand context)
Replace: $1new_text$2

直到对文档没有更多更改为止。

就您而言,您可以使用

找到:&nbsp; (\ sid =“ [^”/]*)/([^“]*” [^&gt;]*&gt;)
替换为$ 1- $ 2

请参阅 regex eggex demo 详细信息

  • - 组1:
    • \ s -whitespace
    • id =“ - 固定的文本
    • [^“]* - 除以外的其他或更多chars” char
  • - 组1的结尾
  • / - a / char
  • - 组2:组2:
    • [^“]* - 除以外的零或更多字符“
    • - a char
    • [^&gt;]* - 除&gt;
    • 以外的零或更多字符

    • &gt; - a &gt; char
  • ) - 第2组的结束。

这将适用于文件搜索,并替换内存搜索并替换功能。

如果您使用使用搜索并替换功能将正则GEGEX应用于单个文档,则可以使用基于外观的模式来实现单个通行证:

查找什么:&nbsp; (?
替换:-

参见此regex degex degex demo 。 详细信息

  • (?&lt; = \ sid =“ [^“]*)
    • \ s -whitespace
    • id =“ - 固定的文本
    • [^“]* - 除以外的其他或更多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

Find:    (left-hand context)text to remove/replace(right-hand context)
Replace: $1new_text$2

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 whitespace
    • id=" - 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 whitespace
    • id=" - 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文