Thread: Regex examples
View Single Post
Old 07-08-2015, 11:25 AM   #472
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Try:

Find:
Code:
(<p(?: [^>]+)?>)(<a [^>]+>[^<>]+</a>)((?:(?!</?a(?: [^>]+)?>).)*)(?=</p>)
Replace:
Code:
\1\2\3\2
Match 1 is the opening paragraph tag. We could use a positive lookbehind and skip replacing that, but then we can't match optional classes and stuff.

Match 2 (in blue above) captures the link and link text:
Code:
<a [^>]+>[^<>]+</a>
Match 3 (in red above) captures the note content, by matching a string that doesn't contain an "a" tag. I lurves my negative lookarounds :
Code:
(?:(?!</?a(?: [^>]+)?>).)*
Finally, we look ahead to spot the closing paragraph tag.

Last edited by eschwartz; 07-08-2015 at 11:28 AM.
eschwartz is offline   Reply With Quote