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
CodePudding user response:
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 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.
CodePudding user response:
I will do match and replace
string pattern = @"id=\""([\w\/] )";
string input = @"<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>";
foreach (Match m in Regex.Matches(input, pattern))
{
string newID = m.Value.Replace("/", "-");
input = input.Replace(m.Value, newID);
}
Console.WriteLine(input);