Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Regex Question

by kcott (Chancellor)
on Nov 26, 2012 at 08:12 UTC ( #1005584=note: print w/replies, xml ) Need Help??


in reply to Regex Question

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

Replies are listed 'Best First'.
Re^2: Regex Question
by Ratazong (Monsignor) on Nov 26, 2012 at 08:30 UTC
    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: note [id://1005584]
help
Chatterbox?
[karlgoethebier]: code><table style="border: 1px solid black;}"></code> same for th and td ?
[shmem]: my scratchpad
[karlgoethebier]: oops!
[shmem]: karlgoethebier: np, got it. But doesn't either :-/
[shmem]: just leaving it as is. After all, enough effort put into that. hrmpf.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2017-07-24 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (356 votes). Check out past polls.