RE: mail header split



> I have an email To field that I want regex on
>
> The email is as so:
>
> 76867487489-bookings@xxxxxxxxxx
>
> Im trying to get the number before the hyphen.
>
> Currently I have it as so
>
> my ($bookrefNumber, $discard) = split('-',$pop3MailContent{'To'});

You don't need to use $discard variable.

You can use splice to capture only the relevant fields.
change it to this...

my ($bookrefNumber) = (split(/-/,$pop3MailContent{'To'}))[0];

> but I dont like this method.

If you do not like this method then use regexp.

A simple regexp without any checks would be(assuming that all email-ids have
the format you specified).

# This will capture all the numbers from the starting till it finds a hyphen
(-).
$pop3MailContent{'To'} =~ /^(\d+)-/;

#$1 contains the pattern matched within the parentheses. If no match is
found then $1 would be undef.
my $bookrefNumber = $1;

And if you want, go through perlre/perlretut. It has lots of information on
regular expressions.

--Ankur


.



Relevant Pages

  • Re: regular expression problem
    ... to have a parenthised expression which is not a capture group. ... But I need it to reject anything that is NOT in parentheses, ... The actual regex that you need seems to be: ... if you want to explicitly discard the capture groups: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: regex: match at least one of two expression
    ... Vincent Mouton wrote in comp.lang.perl.misc: ... the regex. ... > there a better way to capture those 4 possible URL's than the expression ... parentheses, but it isn't clear how you are going to use the captures. ...
    (comp.lang.perl.misc)
  • Re: function to match parenthesis ( and )
    ... I do think you have created a nice debate and have some serious points. ... > prefer RegEx version if you do not need to do this check 100 000 times. ... Hence, not only do you got a cryptic regexp, you also have a cryptic comment ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ... The engine will again try to see if the next character is a B. It ...
    (microsoft.public.dotnet.languages.csharp)