Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Need some character class examples

by aristotle73 (Sexton)
on Jan 03, 2005 at 00:33 UTC ( #418846=perlquestion: print w/replies, xml ) Need Help??
aristotle73 has asked for the wisdom of the Perl Monks concerning the following question:

I was looking over this tutorial Character Class Abbreviations on character class abbreviations and I am a little confused. I do not understand the implementation of these abbreviations. Does anyone know of any examples I could look over (I looked at the pattern matching examples but found them hard to understand as well)? Thx :D

20050102 Edit by ysth: use bracketed link instead of http://perlmonks.org...
Retitled by davido from ambiguous "Need some examples."

Replies are listed 'Best First'.
Re: Need some character class examples
by sgifford (Prior) on Jan 03, 2005 at 02:43 UTC

    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+/.

Re: Need some character class examples
by phenom (Chaplain) on Jan 03, 2005 at 00:54 UTC
    Those will come in handy with regular expressions. You should have perldoc installed on your system, so try: perldoc perlrequick and  perldoc perlretut.
    print "Matched!\n" if($user =~ /^aristotle73$/i);
    Try reading those first. They'll give you a better idea. HTH.
      Thx, that helped a bunch :D

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://418846]
Approved by Errto
help
Chatterbox?
[LanX]: oh thank you! :)
[Eily]: partly because I read tsj as /t͡ʃ/, don't know if that's supposed to be the case
[LanX]: Yeah I think thats Dutch spelling rule
[erix]: but we don't sneeze any K's
[choroba]: we do
[Eily]: that was a sneeze overpowering a cough
[choroba]: Czechs sneeze "Hep-cheek"
vrk adds gingerbread cookies to the platter on the sideboard.
[erix]: I'll give that a try next time
vrk takes a cookie from the platter on the sideboard.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2017-04-24 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (442 votes). Check out past polls.