PDA

View Full Version : Matching min/max occurrences of any character using regular expressions (.)


Omnis
02-11-2007, 10:28 AM
In my Perl CGI script I've been trying to check whether a variable contains a minimum up-to a maximum number of any character like so.

m/^([.\n]{1,19})$/

It doesn't work though, but when I take it out of the square brackets [] as so, without the newline (\n) it works fine.

m/^(.{1,19})$/

Is there a way to include the newline together with the period (.)?

KevinADC
02-11-2007, 08:02 PM
adding the 's' option should do the trick:

m/^(.{1,19})$/s

note: a dot in a character class is just a dot.

The length() function is also useful for what you are trying to do.

Omnis
02-13-2007, 01:35 PM
Thanks KevinADC :thumbsup: the 's' works as intended.

note: a dot in a character class is just a dot.

So does that mean it doesn't have to be escaped in a character class? What characters do have to be escaped in a character class than?

An off-topic question? are you the same Kevin on SitePoint?

KevinADC
02-13-2007, 07:55 PM
I'm not sure of which all meta characters loose their meta meaning in a character class (CC), but the dot is just a dot inside of [.], and doesn't need to be escaped. But it doesn't hurt anything to esape it: [\.]

Yes, I am KevinR on Sitepoint. :)