Device: eb1150 & is that a nook in her pocket, or she just happy to see you?

More fun with regex / wildcards

I'm trying to automate a find and replace routine to change chapter numbers from paragraphs to headings. in other words, currently, they look like this : <p>1</p> and i want them to look like this : <h3>1</h3>

i first tried to do this with wildcards and i have no troubles finding the numbers, but unfortunately if i put <h3>?</h3> in the "replace" box, it replaces the number with "?". not helpful.

A good regex info site is http://www.regular-expressions.info/, and a great regex test site is Regexr. But do note that Regexr use the Perl notation for backreferences ("$#", e.g "$1", "$2"...) while Sigil uses the sed notation ("\#", e.g. "\1", "\2" ).

Device: eb1150 & is that a nook in her pocket, or she just happy to see you?

Quote:

Originally Posted by Valloric

Harry, I don't know what regex flavor you are using, but all the engines that I know of require parentheses for capture groups.

So like this:

Code:

<p>([0-9]*)</p>

And then:

Code:

<h3>\1</h3>

For what Zelda wants, I'd use this:

Code:

<p>(\d+)</p>

With this:

Code:

<h3>\1</h3>

woohoo !! thanks valloric ! that worked perfectly. now when i'm done with all my other corrections i can copy all my html into previous sigil and add in my chapter break divs with a simple find and replace on <h3>. joy.

Quote:

Originally Posted by Valloric

A good regex info site is http://www.regular-expressions.info/, and a great regex test site is Regexr. But do note that Regexr use the Perl notation for backreferences ("$#", e.g "$1", "$2"...) while Sigil uses the sed notation ("\#", e.g. "\1", "\2" ).

and thank you for that, too. i'm sure i'll have plenty of other expressions to find.

Be sure to keep the 'minimal matching' box checked whenever you use regexes (unless you're sure you don't want it). Sigil's support for non-greedy matching is very admirable and makes all the difference.

If you're experimenting with more complex regexes it's always a good idea to address the problem in stages, and save the book under a new name after each replace operation so you can backtrack if something goes wrong.

Device: eb1150 & is that a nook in her pocket, or she just happy to see you?

thanks charleski, that is very sound advice, especially as i have already gotten myself into trouble doing a "replace all" with the wrong replacement... (and that was *before* i started to tackle regex searches).