| [reply] [Watch: Dir/Any] |
Yup. better to make it a bit more restrictive.
s/"(http[^"]+)&(?!amp;)/$1&/g;
| [reply] [Watch: Dir/Any] [d/l] |
| [reply] [Watch: Dir/Any] |
I think the proper way to control this sort of operation -- and keep it from screwing up other entity references (like è and <, etc) is something like this:
s/&(?!\w+;)/&/;
This is just assuming that every valid entity reference that might exist in the original text is limited to alphanumerics and underscores between the initial ampersand and the final semi-colon, which is probably a safe-enough assumption.
But keep a backup of the original. If the data still causes parse errors after this simple edit, they might be different problems you haven't fixed yet, or they might be problems created by this simple edit. Careful diagnosis would be needed in that case. | [reply] [Watch: Dir/Any] [d/l] |
| [reply] [Watch: Dir/Any] |
First, the parens in the pattern part of the regex collect the value we find, then the $1 in the replace puts it back in. In that respect the follwing regex would do nothing but spin cycles;
$line =~ s/(&)/$1/g;
So, in your regex the first set of parens save the & for later use. But you don't use it, instead you find all of the &s and replace them literaly with "(&)" including the parens.
That said, the regex I posted would work only if no other entites existed. Take a look at the other regexs above, they do a better job of 'thinking' ahead, to prevent possible errors in the future.
Somebody could, and I have seen it done, place other entities in the url, such as:
http://www.test.me/test.pl?me=1&you=1&string=Montréal
In this case the regex I posted would tranlate to Montréal. Not what you want.
Edit by castaway: Closed small tag in signature | [reply] [Watch: Dir/Any] [d/l] [select] |