Help needed with regex

Just starting out? Need help? Post your questions and find answers here.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Help needed with regex

Post by Seymour Clufley »

I'm fairly new to Regular Expressions. The goal is to turn this:
<a href="https://yahoo.com">hyperlink "internal" text</a>
into this:
<a href="https://yahoo.com">hyperlink &quot;internal&quot; text</a>
ie. replace all instances of Chr(34) that are not inside HTML tags (of any kind).

Attempting the same thing in Javascript, I found a regex online and adapted it into this, which works perfectly:

Code: Select all

/(?![^<>]*>)"/g
Unfortunately, this doesn't work in PB:

Code: Select all

c34.s = Chr(34)
h.s = "<a href="+c34+"https://yahoo.com"+c34+">hyperlink "+c34+"internal"+c34+" text</a>"
rg.i = CreateRegularExpression(#PB_Any,"/(?![^<>]*>)"+c34+"/g")
h = ReplaceRegularExpression(rg,h,"&quot;"); // replace all c34s that are not inside HTML tags
Debug h
Can anyone tell me what I am doing wrong?
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Help needed with regex

Post by Marc56us »

Hi,

Juste remove / and /g
(in some langages, / and / are start and end of RegEx and g mean global (default mode in PB)

Code: Select all

rg.i = CreateRegularExpression(#PB_Any,"(?![^<>]*>)"+c34+"")
:wink:
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Help needed with regex

Post by normeus »

Code: Select all

c34.s = Chr(34)
h.s = "<a href="+c34+"https://yahoo.com"+c34+">hyperlink "+c34+"internal"+c34+" text</a>"
rg.i = CreateRegularExpression(#PB_Any,"(?![^<>]*>)"+c34)
h = ReplaceRegularExpression(rg,h,"&quot;"); // replace all c34s that are not inside HTML tags
Debug h
no need for "g"


Norm
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Help needed with regex

Post by Seymour Clufley »

Thank you. :)
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply