Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regex Question

by shealyw2 (Initiate)
on Nov 26, 2012 at 05:35 UTC ( #1005559=perlquestion: print w/ replies, xml ) Need Help??
shealyw2 has asked for the wisdom of the Perl Monks concerning the following question:

I have a value coming in from a db. I need to see if the third character in the string is say an 'a'. I am a little confused on how to tell it to ignore all ranges around it and only select the third space.

Comment on Regex Question
Re: Regex Question
by frozenwithjoy (Curate) on Nov 26, 2012 at 05:43 UTC

    I definitely recommend taking an in-depth look at perlretut.

    For your current issue, I would use a regex that matches the beginning of a string (^) followed by 2 of any character (.{2}) and then an a:

    /^.{2}a/
      Thank you, that just made all the other stuff i had read click. Also thanks for the link, always looking for new information to inhale.
Re: Regex Question
by Kenosis (Priest) on Nov 26, 2012 at 05:46 UTC

    You could use substr for this:

    use strict; use warnings; my $string = 'That string is funny!'; if ( substr( $string, 2, 1 ) eq 'a' ) { print 'The third character in $string is an "a".'; }

    But if you're set on a regex, the following will match a string whose third character is "a":

    $string =~ /^..a/
      Thanks, i don't know why i did not think of substring. That defiantly is an option i should have considered.

        You're most welcome, shealyw2!

Re: Regex Question
by kcott (Abbot) on Nov 26, 2012 at 08:12 UTC

    G'day shealyw2,

    index is another option:

    Update: Oops! Left off the POSITION (index STR,SUBSTR,POSITION). Thanks, Ratazong. Here's an improved version.

    $ perl -Mstrict -Mwarnings -E ' my $x = "abc"; say $x if index($x, "a", 2) == 2; my $y = "cba"; say $y if index($y, "a", 2) == 2; my $z = "aba"; say $z if index($z, "a", 2) == 2; ' cba aba
    $ perl -Mstrict -Mwarnings -E ' my $x = "abc"; say $x if index($x, "a") == 2; my $y = "cba"; say $y if index($y, "a") == 2; ' cba

    Note: index is based at zero (1st char = 0; 3rd char = 2)

    -- Ken

      Your index won't work in case there is an a before the third position, e.g. in the string aba. If you (really) want to use index, use the 3-argument-form instead:
      $x = "aba"; print "$x\n" if index($x, "a", 2) == 2;
      HTH, Rata

        ++ Thanks Ratazong. You're absolutely correct. I've updated my node.

        -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005559]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-12-22 03:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls