Reputation: 157
I am very new to regular expressions. I am using UltraEdit, and would like to use regular expressions to make the changes described below.
I have some text in the following pattern:
<Music href="6000111.genre" title="AAA">
<Music format="ditamap" href="000760.rock" title="222"/>
<Music format="ditamap" href="000756.rock" title="333"/>
</Music>
I need to add prefix 'Z' in front of href
with extension .rock
.
href="000760.rock" --> href="Z000760.rock"
The output should look like this:
<Music href="6000111.genre" title="AAA">
<Music format="ditamap" href="Z000760.rock" title="222"/>
<Music format="ditamap" href="Z000756.rock" title="333"/>
</Music>
What would be the regular expression to do this in UltraEdit?
Upvotes: 2
Views: 331
Reputation: 9683
Re-wrote my answer to
The first case I answered is where none of the HREF values already have the X prefix.
Find:
href="([^"]*)\.rock"
And replace:
href="X\1.rock"
Start:
<Music href="6000111.genre" title="AAA">
<Music format="ditamap" href="000760.rock" title="222"/>
<Music format="ditamap" href="000756.rock" title="333"/>
</Music>
Finish:
<Music href="6000111.genre" title="AAA">
<Music format="ditamap" href="X000760.rock" title="222"/>
<Music format="ditamap" href="X000756.rock" title="333"/>
</Music>
Screen shot showing this first result is below.
Breakdown of the regex:
href="([^"]*)\.rock"
href="
- this finds href="
([^"]*)
- this creates the first backreference - tells the engine to look for and remember everything between the brackets: [^"]*
so that we can reference it in the replace part.
[^"]
- this part of the pattern says any character that is not a double quote.[^"]*
is a repetition pattern that says look for zero or more characters that matches the thing just before it (so find zero or more characters that are not a double quote).\.rock"
this defines the rest of the pattern which must be .rock"
\.
. That is because period has a special meaning in a regex and we are telling the regex that we mean a literal dot or period.href="X\1.rock"
href="X
- says to output literally href="X
..\1
- says to replace \1
with the first backreference we created (zero or more characters that are not a double quote)..rock"
- says to output literally .rock"
.
The second case is in response to OP's comment that some of the HREF values already have the X prefix. In this case, change the regex as below.
Find:
href="([^X][^"]*)\.rock"
And replace:
href="X\1.rock"
Start:
<Music href="6000111.genre" title="AAA">
<Music format="ditamap" href="000760.rock" title="222"/>
<Music format="ditamap" href="X000756.rock" title="333"/>
<Music format="ditamap" href="000757.rock" title="444"/>
<Music format="ditamap" href="X000758.rock" title="555"/>
<Music format="ditamap" href="000759.rock" title="666"/>
</Music>
Finish:
<Music href="6000111.genre" title="AAA">
<Music format="ditamap" href="X000760.rock" title="222"/>
<Music format="ditamap" href="X000756.rock" title="333"/>
<Music format="ditamap" href="X000757.rock" title="444"/>
<Music format="ditamap" href="X000758.rock" title="555"/>
<Music format="ditamap" href="X000759.rock" title="666"/>
</Music>
Screen shot showing this second result is below.
Breakdown of the regex:
href="([^X][^"]*)\.rock"
href="
- this finds href="
([^X][^"]*)
- this creates the first backreference - tells the engine to look for and remember everything between the brackets: ([^X][^"]*)*
so that we can reference it in the replace part.
[^X]*
- this part of the pattern says any character that is not an X.[^"]
- this part of the pattern says any character that is not a double quote.[^"]*
is a repetition pattern that says look for zero or more characters that matches the thing just before it (so find zero or more characters that are not a double quote).\.rock"
this defines the rest of the pattern which must be .rock"
\.
. That is because period has a special meaning in a regex and we are telling the regex that we mean a literal dot or period.href="X\1.rock"
href="X
- says to output literally href="X
..\1
- says to replace \1
with the first backreference we created (zero or more characters that are not a double quote)..rock"
- says to output literally .rock"
.
Upvotes: 2
Reputation: 91415
I'm not sure for Ultraedit, but I assume it's close to notepad++:
Find what: (href=")(.+?\.rock")
Replace with: $1X$2
X
or Z
as it's not clear in your question.
Upvotes: 1