Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Perl regular expression

by danny0085 (Sexton)
on Apr 30, 2012 at 11:36 UTC ( #968044=perlquestion: print w/ replies, xml ) Need Help??
danny0085 has asked for the wisdom of the Perl Monks concerning the following question:

I have a function that read hotmail inbox and return the confirmation link but sometimes return something like:

https://domain.com/account/confirm_email/JodiImus40/589HC-CG26G-133578 Once you confirm, you will have full access and all future n= otifications will be sent to this email address.

How can I erase Everything after the code 589HC-CG26G-133578

PD: The code is always different Thanks

Comment on Perl regular expression
Download Code
Re: Perl regular expression
by Anonymous Monk on Apr 30, 2012 at 11:48 UTC
    The expression
    'https://domain.com/account/confirm_email/JodiImus40/589HC-CG26G-13357 +8 Once you confirm, you will have full access and all future n= otifi +cations will be sent to this email address.' =~ s/\K Once.*//r
    returns the front part.

    This is a trivial question. I suggest you learn the language properly first on your own: http://learn.perl.org/

      This is a trivial question. I suggest you learn the language properly first on your own: http://learn.perl.org/

      Trivial? You use two modern regex features not mentioned in the faq or introductory text, and certainly not mentioned on learn.perl.org

Re: Perl regular expression
by JavaFan (Canon) on Apr 30, 2012 at 16:06 UTC
    A couple of ways:
    $str =~ s/589HC-CG26G-133578.*/589HC-CG26G-133578/; $str =~ s/589HC-CG26G-133578\K.*//; $str =~ s/(589HC-CG26G-133578).*/$1/; ($str) = $str =~ /(.*589HC-CG26G-133578)/; ($str) = $str =~ /.*589HC-CG26G-133578/g; $str = substr($str, 0, index($str, "589HC-CG26G-133578") + length("589 +HC-CG26G-133578")); substr($str, index($str, "589HC-CG26G-133578") + length("589HC-CG26G-1 +33578"), -1) = ""; $str = join "", (split /(589HC-CG26G-133578)/, $str)[0, 1];
Re: Perl regular expression
by Marshall (Prior) on Apr 30, 2012 at 17:47 UTC
    Appears that something simple will work. I don't think a URL can have a space in it, so capture all non-space stuff at the beginning (maybe allow for some leading spaces?). Whatever the message is at the end, it will be ignored - doesn't really matter what the URL is (or the number at the end). Note it is important to anchor the regex to the start of the string.
    #!/usr/bin/perl -w use strict; my $line = 'https://domain.com/account/confirm_email/JodiImus40/589HC- +CG26G-133578 Once you confirm, you will have full access and all futu +re n= otifications will be sent to this email address.'; #just print the first URL part my ($url) = $line =~ /^\s*(\S+)/; print "$url\n" if $url; #https://domain.com/account/confirm_email/JodiImus40/589HC-CG26G-13357 +8 #edit $line to remove all but the first URL part $line =~ s/^\s*(\S+).*/$1/; print $line; #https://domain.com/account/confirm_email/JodiImus40/589HC-CG26G-13357 +8

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-09-22 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls