Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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;

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2018-06-25 09:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.