Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: regex for string

by arkturuz (Curate)
on Aug 18, 2009 at 08:44 UTC ( #789388=note: print w/ replies, xml ) Need Help??

in reply to regex for string

Strictly using regex you can do it like this:

my $str = ""; if ($str =~ / ^(.{1}) # first character .* # the rest of the string (.+)$ # minus the last character /x) { print $1, ' ', $2, "\n"; }
Normally you would do this using substr:
my $f = substr($str, 0, 1); my $l = substr($str, length($str)-1, 1); print $f, ' ', $l;

Comment on Re: regex for string
Select or Download Code
Replies are listed 'Best First'.
Re^2: regex for string
by moritz (Cardinal) on Aug 18, 2009 at 08:48 UTC
    This will fail for a string consisting of a single character, or a string which contains newlines. The first can be fixed with a look-ahead, the second with /s.
    Perl 6 projects - links to (nearly) everything that is Perl 6.
Re^2: regex for string
by saranperl (Initiate) on Aug 18, 2009 at 09:01 UTC
    ya its working fine thank you. why it is not working to giving like this
      It won't match what you want because the '*' quantifier is greedy, and the '?' can match 0 or 1 times. So, the '*' matches all of the it can, and the last '?' matches 0 times; so the match is successful. You have to force '*' to give something back to the rest of the regex by replacing '?' with '{1}' for example.
        good explaination... thanks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://789388]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-11-30 23:26 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (788 votes), past polls