Regex find and replace a digit

To find only a single digit (such as “1”, or “2”) and replace (only) it, you have to add a plus after \d.

Something like:

(\d+)

(to find it) and

\1

to replace it.

For example you have to add a new element to these lines

1 some first text 
3 some other text
8 other text

You can use this regex

find: (\d+) replace: \n\1

as a result you will get


1 some first text 

3 some other text

8 other text

Regex for first lowercase letter

If you have en ebook that split badly a paragraph, you can try (to merge the bad splitted paragraph), as find

</p>\n\n\s\s<p>([a-z])

so that a paragraph beginning with a lowercase word be merged with the previous one.

But in Calibre editor you must check “case sensitive”, otherwise [a-z] will be read as [A-Z], and all first letters (lower and upper case) will be selected.

And as replace you could try

 \1

In this way you keep whatever letter is found with ([a-z]) in the result.

Regex for “space”

It can happen that you want replace a double space before <p>: use the \s. I.g. to replace

,</p>

<p>

You should use this find: ,</p>\n\n\s\s</p>

Regex replace tags around digits

If you would replace all <p>[some digits, such as "10", or "23", or "348"]</p>, you could use this regex code:

search: <p>(.*?\d{2})</p>

replace: <h6>\1</h6>

Indeed (.*?) is whatever, and (.*?\d{2}) is whatever number (with more than two digits).

regex multiple replacements

You could have to replace several “whatever”, f.e. in a sql file I had to replace many columns with dates, such as ‘2005-08-15’ splitting them in ‘2005’, ’08’, ’15’.

I manage to do it with this code:

find NULL, '(.*?)-(.*?)-(.*?)', NULL, NULL, NULL
replace NULL, '\1', '\2', '\3', NULL

In this way you can replace '2005-08-15', NULL, NULL, NULL with '2005', '08', '15', NULL, and likewise all the like, such as '2012-06-23', NULL, NULL, NULL with '2012', '06', '23', NULL and so on.

But, be careful: make a backup of your file before.

another case: if you want replace several digits with the same digits formatted, you can try something like this code:

search: ^    (\d{2})$
replace: <h2>cap. \1</h2>\n

regex replace html tags

In exporting a odt file to epub LibreOffice can make many mistakes, such as get a 2nd level title not with <h2>, but with <p class=”para0″>. To fix this error, you can use regex, in this way:

find: <p class="para0">(.*?)</p>
replace: <h2>\1</h2>

and so on for similar cases.

regex “whatever”

If you want to select “Whatever” (word or character), regardless of its length, you can simply use

(.*?)

For example if you want delete all the words between <span> and </span>, as in the following row

many words <span>many other words here</span> other words

you can use

delete <span>(.*?)</span>. 

The result will be:

many words other words

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)