Re: warning using a regexp
by broquaint (Abbot) on Jan 10, 2003 at 11:10 UTC
|
I get this warning
If you get a warning in perl and are unsure of what it means check out man perldiag for more information
false [] range ""%s"" in regexp
(W regexp) A character class range must start and end at a literal character,
not another character class like "\d" or "[:alpha:]". The "-" in your false
range is interpreted as a literal "-". Consider quoting the "-", "\-". See
the perlre manpage.
HTH
_________ broquaint
| [reply] |
|
|
I looked in perlre, but forgot about perldiag, thanx for the advice
| [reply] |
Re: warning using a regexp
by davorg (Chancellor) on Jan 10, 2003 at 11:13 UTC
|
If you get a warning that you don't understand it's always worth adding "use diagnostics" to your program to get a more details error. You'll see something like this:
False [] range "%s" in regex; marked by <-- HERE in m/%s/
(W regexp) A character class range must start and end at a literal character, not another character class like \d or :alpha:. The "-" in your false range is interpreted as a literal "-". Consider quoting the "-", "\-". The <-- HERE shows in the regular expression about where the problem was discovered. See perlre.
--
<http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] |
|
|
Dave
just wanted to say thanks for the heads up on 'use diagnostics' - never seen it before and have just tried it out. As a novice that could be loads of help in the future!
elbow
| [reply] |
Re: warning using a regexp
by Enlil (Parson) on Jan 10, 2003 at 11:43 UTC
|
Others have mentioned quoting the - so you don't get a false range. What is happening is this (I realize I am probably beating a dead horse by this time), anything within the [] is part of a character class and so if you put something like [a-z], the regex engine will interpret this as anything from a to z (e.g, a,b,c,d,e,f,g), in order to save the progammer some work (i.e. having to type each and ever character). So what you end up needing to do is escape the "-" if you want it as part of your character class, so it is not misiterpreted as a range between the character on its left and the one on its right. Alternatively, if the "-" is the first or last thing in your square brackets then it will also be taken as part of that class, as the regex engine realizes that its position cannot possibly be interpreted as part of a class range. for example: $params{city} =~ /^[-\w'\.\*]*$/ and do_stuff() ;
or$params{city} =~ /^[\w'\.\*-]*$/ and do_stuff() ;
-enlil | [reply] [d/l] [select] |
Re: warning using a regexp
by Coruscate (Sexton) on Jan 10, 2003 at 11:11 UTC
|
/^[\w-'\.\*]*$/
Look at the first few chars within that character class. \w-' the hyphen starts a range (ie: a-z), and whitespace to apostrophe is not a valid range. Perhaps you meant to backslash the hyphen, just as you had to do with the period and the star. Question: do your city names honestly have periods and asterixes (sp?) in them? Perhaps you even want to remove the backslahes from the period and asterix. It would be helpful if you said exactly what you want to check for in the regex.
# The way you might want your regex (will remove error)
/^[\w-\'\.\*]*$/
| [reply] [d/l] [select] |
|
|
Eh, no, that will still error. Try:
/^[-\w'.*]*$/
Abigail | [reply] [d/l] |
|
|
in fact without warning the first regexp worked... I don't know how but it did....
I wrote that :
/^[\w\-'.*]*$/
Would that be good ? It seems to be but I'm cautious now....
| [reply] [d/l] |
|
|
Alright I don't need the asterix but I do need the '.
I still don't get why I should escape ' since it's not supposed to be a funny char, is it ?
And by the way, I didn't fully understood broquaint's explanation (I'm french and I might not understand something properly...)
| [reply] |
|
|
All right I looked again at perlre and I'm sorry I forgot - was used to define a range. Now I understand why I have to backslash it.
It's a wonder anything worked at all beforehand :-)
| [reply] |