Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Character Class Abbreviations

by Terminal (Initiate)
on Dec 23, 2005 at 21:29 UTC ( #518833=note: print w/replies, xml ) Need Help??

in reply to Character Class Abbreviations

Perhaps you could show some examples of these? I'm a bit confused...
I tried
if ($_ =~ [[en]]) { print "yes\n";} else { print "no\n";}
Didn't work :( Always printed no, even when I had "en" in the document :(

Code tags added by Arunbear

Replies are listed 'Best First'.
Re^2: Character Class Abbreviations
by planetscape (Chancellor) on Dec 24, 2005 at 00:19 UTC

    In replying to a 6-yr old node, your question is in danger of flying beneath everyone's radar. Better to post under Seekers of Perl Wisdom. To get an idea of how this site works, I recommend looking at the "Welcome to the Monastery" section of the Tutorials page.

    You might wish to check out some of the references listed here: Re: regexp: extracting info

    In your example, it sounds to me more like you are trying to match the literal string "en". But let's assume for a moment you really want a character class...

    One of the tools I didn't mention in the writeup above is the simple "patten test" program from Learning Perl, 3rd Ed. I often use this when first constructing a regex because it's simple, easy to edit, and I get immediate feedback. So let's start with that program, modified slightly to match the character class [en] .

    # From: Schwartz & Phoenix: Learning Perl, 3rd Ed (The Llama), pp. 103 use strict; while (<>) { chomp; if (/[en]/) { print "Matched: |$`<$&>$'|\n"; } else { print "No match.\n"; } }

    Let's assume that this is our "test" file:

    English French Spanish German Aramaic Arabic

    Where and how the character class matches may surprise you:

    Matched: |E<n>glish| Matched: |Fr<e>nch| Matched: |Spa<n>ish| Matched: |G<e>rman| No match. No match. No match.

    If you were expecting output more like the following, matching the string "en":

    No match. Matched: |Fr<en>ch| No match. No match. No match. No match. No match.

    You will need to change the program as follows:

    # From: Schwartz & Phoenix: Learning Perl, 3rd Ed (The Llama), pp. 103 use strict; while (<>) { chomp; if (/en/) { print "Matched: |$`<$&>$'|\n"; } else { print "No match.\n"; } }



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://518833]
[Discipulus]: good morning choroba!
[Corion]: Hi choroba, Discipulus! I hope you had a good weekend
erix looks for a red-herring joke but can't find one
Discipulus has worked physically and he rediscovered he is no more wont..
erix instead casts a baleful eye on another mysql q.
[Corion]: erix: I'm not sure if it really is MySQL - maybe MS SQL also has LIMIT 0

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2017-01-23 09:24 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (192 votes). Check out past polls.