Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Regex help

by perlnoobster (Sexton)
on Dec 11, 2012 at 11:14 UTC ( #1008266=perlquestion: print w/replies, xml ) Need Help??
perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks!

I was wondering if someone could help me with the following issue that i'm facing, I have a string below that is generated into a text file, there are hundreds of similar text strings but all I want to retain is what ever is after the dp/

current output: -plastic-Protector/dp/B00074658

ideal output: B00074658

is this possible? if so can someone help me?

thank you

Replies are listed 'Best First'.
Re: Regex help
by jwkrahn (Monsignor) on Dec 11, 2012 at 11:29 UTC
    my $str = '-plastic-Protector/dp/B00074658'; $str =~ s!.*/dp/!!; print "$str\n";
      Thank you everyone, all methods worked great :) I do have one slight niggle though, If the string contains the value AA1X32ALFC60JT8 then can this entire row including the /dp be excluded? e.g can -plastic-protector-AA1X32ALFC60JT8/dp/B00074658 be removed from the results? Thank you :)

        See the specific grep-map code in my other answer. For this requirement, you would simply add a negative match to the grep block:

        my @ids = grep { defined $_ && $_ !~ m{AA1X32ALFC60JT8} } map { match_ +id($_) } @lines_from_file;

            All Perl:   MT, NLP, NLU

Re: Regex help
by PetaMem (Priest) on Dec 11, 2012 at 11:43 UTC

    This is one of the easiest tasks for Perl. Contrary to the one-liner that would do, some more verbose code:

    # let's define a sub that will return the matched id IF there was a ma +tch, undef else sub match_id { my $str = shift; if ($str =~ m{dp/(.\d+?)\z}) { # we assume our id is "some charact +er followed by digits" return "$1\n"; # will return id IF we had a match } return; # undefined if there was no match } # assume you have these strings one in a line - or not # and read the file to a list (one line = one list element) # then you can get a list of matches like so: my @ids = grep { defined $_ } map { match_id($_) } @lines_from_file;

    If you have the strings scattered around your file, possibly several on one line, you better use the global modifier and a while loop. You also should tighten your regex then - specifying the ID more exact:

    while ($file_content =~ m{dp/(.\d+?)}g) { # we have to omit the \z print "Found one: $1\n"; }

    E.g. if you knew, all your Id's started with an upper-case letter and have always 8 digits, that would be


        All Perl:   MT, NLP, NLU

Re: Regex help
by 2teez (Vicar) on Dec 11, 2012 at 11:24 UTC

    Something like this:

    my $str='-plastic-Protector/dp/B00074658'; if($str =~m/dp\/(.+?)$/){ print $1,$/; # print B00074658 }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
TMTOWTDI (Re: Regex help)
by space_monk (Chaplain) on Dec 11, 2012 at 21:02 UTC

    You may want to consider alternative solutions; split or maybe even rindex would do the job... :-)

    A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Regex help
by TJPride (Pilgrim) on Dec 11, 2012 at 11:24 UTC
    $var =~ s/.*\///;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008266]
Approved by 2teez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-06-23 13:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.