| Author |
YARQ: Yet Another Regex Question
|
|
| Mathew Snyder 2007-05-16, 7:58 am |
| I have a trouble ticket application that uses a regex to find a piece of
information in an incoming email and auto populate a field if it is found. The
line it will be looking for is
CUSTOMER ENVIRONMENT customer_name
where customer_name will never have a space making it one word. If I just want
to pull from the line the customer_name would my regex look like
$MatchString = "CUSTOMER ENVIRONMENT\s+(\w)"
For what it's worth the line that will handle this is
$found = ($Transaction->Attachments->First->Content =~ /$MatchString/m);
I'm guessing that when used in an assignment like this, $1 will be used as the
value. The contents of (\w) in this case. Is that correct?
Mathew
--
Keep up with me and what I'm up to: http://theillien.blogspot.com
| |
| Chas Owens 2007-05-16, 7:58 am |
| On 5/16/07, Mathew Snyder <theillien@yahoo.com> wrote:
> I have a trouble ticket application that uses a regex to find a piece of
> information in an incoming email and auto populate a field if it is found. The
> line it will be looking for is
> CUSTOMER ENVIRONMENT customer_name
> where customer_name will never have a space making it one word. If I just want
> to pull from the line the customer_name would my regex look like
> $MatchString = "CUSTOMER ENVIRONMENT\s+(\w)"
Bad idea. Use qr() instead.
>
> For what it's worth the line that will handle this is
> $found = ($Transaction->Attachments->First->Content =~ /$MatchString/m);
> I'm guessing that when used in an assignment like this, $1 will be used as the
> value. The contents of (\w) in this case. Is that correct?
snip
Yes, the $1 match variable will hold the match if $found is true. A
common idiom is therefore
my $name;
my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/;
if ($Transaction->Attachments->First->Content =~ /$regex) {
$name = $1;
} else {
die "could not find name";
}
Another way to write this is
my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/;
my ($name) = $Transaction->Attachments->First->Content =~ /$regex/
or die "could not find name";
| |
| Mathew 2007-05-16, 7:58 am |
|
Chas Owens wrote:
> On 5/16/07, Mathew Snyder <theillien@yahoo.com> wrote:
>
> Bad idea. Use qr() instead.
>
> snip
>
> Yes, the $1 match variable will hold the match if $found is true. A
> common idiom is therefore
>
> my $name;
> my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/;
> if ($Transaction->Attachments->First->Content =~ /$regex) {
> $name = $1;
> } else {
> die "could not find name";
> }
>
> Another way to write this is
>
> my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/;
> my ($name) = $Transaction->Attachments->First->Content =~ /$regex/
> or die "could not find name";
>
What does gr() do?
Mathew
| |
| Chas Owens 2007-05-16, 6:59 pm |
| On 5/16/07, Mathew <theillien@yahoo.com> wrote:
snip
> What does gr() do?
>
> Mathew
>
qr not gr. It is the quote regex operator.
from perldoc perlop
qr/STRING/imosx
This operator quotes (and possibly compiles) its STRING as a
regular expression. STRING is interpolated the same way as
PATTERN in "m/PATTERN/". If "'" is used as the delimiter, no
interpolation is done. Returns a Perl value which may be used
instead of the corresponding "/STRING/imosx" expression.
| |
| Jeff Pang 2007-05-16, 6:59 pm |
| Mathew 写道:
>
> What does gr() do?
>
It's "qr" not "gr".
See "perldoc perlop" and look for "qr/STRING/imosx".
--
http://home.arcor.de/jeffpang/
| |
| Mathew 2007-05-16, 6:59 pm |
|
Chas Owens wrote:
> On 5/16/07, Mathew <theillien@yahoo.com> wrote:
> snip
>
> qr not gr. It is the quote regex operator.
>
> from perldoc perlop
> qr/STRING/imosx
> This operator quotes (and possibly compiles) its STRING as a
> regular expression. STRING is interpolated the same way as
> PATTERN in "m/PATTERN/". If "'" is used as the delimiter, no
> interpolation is done. Returns a Perl value which may be
> used
> instead of the corresponding "/STRING/imosx" expression.
>
Ahh, yes that would certainly work better. One of my concerns was using
a bare string containing a space. While it may not break it could cause
problems down the road. Something I'd like to avoid.
Thanks
Mathew
|
|
|
|