Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Need some character class examples

by sgifford (Prior)
on Jan 03, 2005 at 02:43 UTC ( #418859=note: print w/replies, xml ) Need Help??


in reply to Need some character class examples

Character classes are a way of saying this character or this other one or this other one or this other one. For example, [abc] matches a or b or c, as does the range [a-c]. The abbreviations are just shorthand for frequently used character classes. \d is shorthand for 0 or 1 or 2 or ... or 8 or 9---exactly the same as [0-9].

So, if you required that a username start with a letter then be made up of letters or numbers, you could use /[a-zA-Z]\d*/. If you wanted to match two words seperated by one or more spaces, you could use /\w+\s+\w+/.

Inverted character classes mean anything but this character or this other one or this other one. For example, [^0-9] matches anything except a digit. The abbreviation for this is \D.

So, if you want to see if a string contained any non-word characters, you could use /\W/; if you wanted to match 3 sets of digits followed by nondigits, you could use /\d+\D+\d+\D+\d+\D+/.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://418859]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-07-16 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (330 votes). Check out past polls.

    Notices?