regex replace keeping a part of a string

Today I managed to replace (in html file links and anchors) absolute paths with relative ones.

I needed to keep the anchors and links number.

I did so:

search: href="#_ftn([0-9])">
replace: href="#_ftnref\1" id="_ftn\1">

In this way href=”#_ftn1 was replaced by  href="#_ftnref1" id="_ftn1", href=”#_ftn2 was replaced by  href="#_ftnref2" id="_ftn2", and so on.

For numbers with two digits

searchhref="#_ftn([0-9][0-9])">
replacehref="#_ftnref\1" id="_ftn\1">

Only the search string is to be modified, if you have more digits (f.e. [0-9][0-9][0-9][0-9] if you have numbers with four digits, such as 1276), not the replace one (so don’t write \1\1).

digits within brackets

To replace digits within brackets you have to escape the brackets with ‘\’, thus: ‘\(‘, or ‘\[‘. For example, if you plan to convert every ‘(some digits) ‘ with ‘</p><p>(some digits)’, you should use this code:

find: \(([0-9])\)
replace: </p><p>(\1)

Lascia un commento

Il tuo indirizzo email non sarĂ  pubblicato. I campi obbligatori sono contrassegnati *