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

Regular expression question

by megaurav2002 (Monk)
on May 03, 2007 at 10:29 UTC ( [id://613345]=perlquestion: print w/replies, xml ) Need Help??

megaurav2002 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, i have two types of string as following: id_1_1 and id_2. i want to extract 2, because after 2 no more _ exists. Can anyone suggest me some re for this. Thanks

Replies are listed 'Best First'.
Re: Regular expression question
by GrandFather (Saint) on May 03, 2007 at 10:39 UTC

    It is generally a good idea to provide a little code to demonstrate your issue and to show that you have put at least some effort in. It also helps if you show us what you expected to happen and what actually happens so we have a clear understanding of the problem and the required outcome. This is especially important when you are not a native English speaker or have difficulty describing the problem in prose.

    It may be that what you are after is something like:

    use strict; use warnings; my @strs = qw(id_1_1 id_2 banana id_1_2_); for my $id (@strs) { if ($id =~ /(\d)(?!_)/) { print "Extracted $1 from $id\n"; } else { print "no match for $id\n"; } }

    Prints:

    Extracted 1 from id_1_1 Extracted 2 from id_2 no match for banana no match for id_1_2_

    DWIM is Perl's answer to Gödel
      Hi, Thanks it's working.. Sorry about my english, Hope you don't mind...
Re: Regular expression question
by Samy_rio (Vicar) on May 03, 2007 at 10:40 UTC

    Hi megaurav2002, if I understood your question correctly then the following would will help you.

    use strict; use warnings; my @array = qw(id_1_1 id_2); for (@array){ print "$1\n" if (m/id\_(\d+)(?!\_)/); } __END__ Output: ------ 2

    Also look into How (Not) To Ask A Question

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Regular expression question
by shigetsu (Hermit) on May 03, 2007 at 10:37 UTC

    Sorry for the confusion I caused.

    I misinterpreted your question and thus I thought it's of no use if I leave my false statements in (in order to not distract others from the essence of things).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-03-29 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found