Ask a Question related to PERL Miscellaneous, Design and Development.
-
Neil Shadrach #1
Difference between two regular expressions
Why do the two 'print' lines behave differently in the following?
#!/usr/bin/perl -w
use strict;
use warnings;
$/.=$/;
while(<DATA>)
{
print "\nA:1[$1]\nA:2[$2]" if /bindings: (?:(.+ => .+)\n?\s*)(?:(.+ => .+\n?\s*))/;
print "\nB:1[$1]\nB:2[$2]" if /bindings: (?:(.+ => .+)\n?\s*){2}/;
}
__DATA__
community: public
enterprise: 9.9.9.9.9.9.999.9.9.9.9
agent addr: 999.99.99.999
agent name: ab.cd.ef.gh
generic ID: 9
specific ID: 999
uptime: 9:99:99
bindings: 9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached maximum size
9.9.9.9.9.9.999.9.9.9.9.9 => local_storage
###
!/tmp/demo.pl
A:1[9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached maximum size]
A:2[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage
]Use of uninitialized value in concatenation (.) at /tmp/demo.pl line 11, <DATA> chunk 1.
B:1[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage]
B:2[]
Neil Shadrach Guest
-
Regular Expressions :(
Hello all! I was trying to write down a function to remove all the bbcode tags from a post before showing it (non in the phpbb, in another... -
PHP regular expressions
Hello, I am writing an elearning software with php. I need a script to recognize any inut of the type ab,aabb,aaabbb,aaaabbbb..... And so on.... -
[PHP] Q on Regular Expressions
* Thus wrote jsWalter (jsWalter@torres.ws): have you looked at strtotime()? http://php.net/strtotime The Date formats it can find are defined... -
[PHP-DEV] PHP regular expressions
Hello. In regex/utils.h there is a definition for DUPMAX: #ifdef _POSIX2_RE_DUP_MAX #define DUPMAX _POSIX2_RE_DUP_MAX #else #define DUPMAX 255... -
Regular Expressions....HELP!
I'm thinking that I need to use a regular expression to make this work, but I'm not expert at them. What I'd like to do is be able to search a... -
Anno Siegel #2
Re: Difference between two regular expressions
Neil Shadrach <neil.shadrach@corryn.com> wrote in comp.lang.perl.misc:
The first patten contains two pairs of capturing parentheses, the second>
> Why do the two 'print' lines behave differently in the following?
>
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
> $/.=$/;
>
> while(<DATA>)
> {
> print "\nA:1[$1]\nA:2[$2]" if /bindings: (?:(.+ => .+)\n?\s*)(?:(.+
> => .+\n?\s*))/;
> print "\nB:1[$1]\nB:2[$2]" if /bindings: (?:(.+ => .+)\n?\s*){2}/;
> }
>
> __DATA__
> community: public
> enterprise: 9.9.9.9.9.9.999.9.9.9.9
> agent addr: 999.99.99.999
> agent name: ab.cd.ef.gh
> generic ID: 9
> specific ID: 999
> uptime: 9:99:99
> bindings: 9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached
> maximum size
> 9.9.9.9.9.9.999.9.9.9.9.9 => local_storage
> ###
> !/tmp/demo.pl
>
> A:1[9.9.9.9.9.9.9.9.9 => Warning: /local/file has reached maximum size]
> A:2[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage
>
> ]Use of uninitialized value in concatenation (.) at /tmp/demo.pl line
> 11, <DATA> chunk 1.
>
> B:1[9.9.9.9.9.9.999.9.9.9.9.9 => local_storage]
> B:2[]
contains only one. A regex sets only as many $<n> variables as it
captures.
Your problem is obfuscated by a lot of accidential circumstances,
like multiline matching with a non-standard $/, and more. Reduce
it to the simplest terms that still exhibit the problem:
$_ = 'tuttut';
print "$1, $2\n" if /(tut)(tut)/;
print "$1, $2\n" if /(tut){2}/;
Try this technique before posting -- you'll either solve your problem
yourself, or you'll be able to present a much more appealing question.
Anno
Anno Siegel Guest



Reply With Quote

