regexp avoiding applying 2 times & -> &

U

Une bévue

i want to find a regexp avoiding to apply two times a entity transform
for the cases :

' or '
&
>
<
"

i found a way :

txtout=txtin.gsub(/&[^#aglq]/, "&")

which works, by chance, over the cases i have ))

the prob comes because the negate symbol ^ applies to the list of chars,
each individualy, following it. Then if i have a string including &a
(with my regex) the & will not be transformed to &, which is
wrong...

i'd like to know a way to say :

match & not followed by :
#\d\d;
amp;
gt;
lt;
quot;

i think this is only possible using back reference, that's to say ;
after having matched amp; (for example) negate the previous matching of
&...

but i don't know how to do that ))
 
R

Ross Bamford

match & not followed by :
#\d\d;
amp;
gt;
lt;
quot;

Does this work?

rxp = /&(?!(#\d\d|amp|quot|lt|gt);)/

"&" =~ rxp
# => nil
"!" =~ rxp
# => nil
"<" =~ rxp
# => nil

"&" =~ rxp
# => 0
"!" =~ rxp
# => 0
"&how!" =~ rxp
# => 0
"&quo" =~ rxp
# => 0

Hope that helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top