Mailing List

Nested
is "@+" a magic var?
User: afbach
Date: 6/23/2010 10:51 am
Views: 201
Rating: 0
Hey,

I'm teaching a class for work and we were doing an example using
regular expressions. We had a simple email validation script and a
user ended up adding a plus to the '@' in the "middle" of the RE.  It
failed. Escaping the '@' worked and playing w/ it, it appears @+ isn't
"one or more @ chars" as one might think. Capturing what it matches
gets an empty string.
For example:

$ perl -w -e 'print "-@+ -\n"'
- -

It goes away, as if it were a global var (like @_ maybe?).  I can't
find it in my books but ...

a
--

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: is "@+" a magic var?
User: hoelzro
Date: 6/23/2010 11:12 am
Views: 0
Rating: 0
A good rule of thumb in regexes is to always escape sigils like '$' and
'@'.  @+ *is* in fact a special variable; it contains the offsets of
the submatches from the last successful match.

http://perldoc.perl.org/perlvar.html#%40%2b

-Rob

On Wed, 23 Jun 2010 10:51:02 -0500
wrote:

> afbach wrote:
> Hey,
>
> I'm teaching a class for work and we were doing an example using
> regular expressions. We had a simple email validation script and a
> user ended up adding a plus to the '@' in the "middle" of the RE.  It
> failed. Escaping the '@' worked and playing w/ it, it appears @+ isn't
> "one or more @ chars" as one might think. Capturing what it matches
> gets an empty string.
> For example:
>
> $ perl -w -e 'print "-@+ -\n"'
> - -
>
> It goes away, as if it were a global var (like @_ maybe?).  I can't
> find it in my books but ...
>
> a
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org

Re: is "@+" a magic var?
User: chuckbutler
Date: 6/23/2010 11:16 am
Views: 0
Rating: 0
A-

There is a global variable '@+'.  From perlvar:

 @LAST_MATCH_END
 @+      This array holds the offsets of the ends of the last successful
         submatches in the currently active dynamic scope. $+[0] is the
         offset into the string of the end of the entire match. This is
         the same value as what the "pos" function returns when called on
         the variable that was matched against. The *n*th element of this
         array holds the offset of the *n*th submatch, so $+[1] is the
         offset past where $1 ends, $+[2] the offset past where $2 ends,
         and so on. You can use $#+ to determine how many subgroups were
         in the last successful match. See the examples given for the
         "@-" variable.

Thanks.

-c

Chuck Butler
mailto:chuckbutler@usa.com


-----Original Message-----
From: afbach@gmail.com
To: chuckbutler@usa.com
Sent: Wed, Jun 23, 2010 10:51 am
Subject: [MadTalk] is "@+" a magic var?

afbach wrote:
Hey,

I'm teaching a class for work and we were doing an example using
regular expressions. We had a simple email validation script and a
user ended up adding a plus to the '@' in the "middle" of the RE.  It
failed. Escaping the '@' worked and playing w/ it, it appears @+ isn't
"one or more @ chars" as one might think. Capturing what it matches
gets an empty string.
For example:

$ perl -w -e 'print "-@+ -\n"'
- -

It goes away, as if it were a global var (like @_ maybe?).  I can't
find it in my books but ...

a
--

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk


Madison Area Perl Mongers - MadMongers
http://www.madmongers.org
Re: is "@+" a magic var?
User: preaction
Date: 6/23/2010 12:14 pm
Views: 1
Rating: 0
It's one of the magic regexp vars like $` and $&, so it's probably documented in perldoc perlre or perldoc perlvar
Re: is "@+" a magic var?
User: afbach
Date: 6/23/2010 2:19 pm
Views: 0
Rating: 0
On Wed, Jun 23, 2010 at 11:16 AM,   wrote:
> There is a global variable '@+'.  From perlvar:
>  @LAST_MATCH_END

That's it! Thanks.  I did check on list of the Magic vars and didn't
see it and google didn't get me anything (hmmm is "@+" a google magic
string too? ;-) but ... I guess I'm less likely to start escaping
everything, I'd rather find out than just be safe but, as in this
case, it would have been a good idea.

Thanks all.

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: is "@+" a magic var?
User: hoelzro
Date: 6/23/2010 2:23 pm
Views: 0
Rating: 0
Next time you need to find out what a magic variable does (or whether
it even exists), try perlvar:

http://perldoc.perl.org/perlvar.html

-Rob

On Wed, 23 Jun 2010 14:19:30 -0500
wrote:

> afbach wrote:
> On Wed, Jun 23, 2010 at 11:16 AM,   wrote:
> > There is a global variable '@+'.  From perlvar:
> >  @LAST_MATCH_END
>
> That's it! Thanks.  I did check on list of the Magic vars and didn't
> see it and google didn't get me anything (hmmm is "@+" a google magic
> string too? ;-) but ... I guess I'm less likely to start escaping
> everything, I'd rather find out than just be safe but, as in this
> case, it would have been a good idea.
>
> Thanks all.
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org

PreviousNext
Madison Area Perl Mongers