regex question about ?, *, and $1

regex question about ?, *, and $1

am 19.12.2007 11:54:28 von vorticitywolfe

Alright, this is confusing me:

$string = 2340Z 4SL -ABCD PS T1203045;

if $string =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|PS|PT)*?/g{
print $1,$2,$3,$4;
}

This works like I want it returns values for $1,$2,$3 and $4

However,
if you change it to this
$string = 2340Z 4SL PS T1203045;

It doesn't return anything for $1,$2,$3 or $4

Why?

I thought that any match followed by a " * " says that it matches 0 or
more, so if it's not matched it's undefined but the parenthesis still
"hold" it's place, i.e. $1 is still associated with the first
parenthetical match and doesn't switch to the second (AB|YZ) in this
case.

So in the end I would like this to work like this
$string = 2340Z 4SL -ABCD PS T1203045; ### print -ABCD PS
$string = 2340Z 4SL PS T1203045; ### print PS
$string = 2340Z 4SL -CD T1203045; ### print -CD

I have some logic to do all of that, but it's the regex that is
sticking me, particularly the * vs. ? vs. what is returned.

Thanks for the help in advance!

Re: regex question about ?, *, and $1

am 19.12.2007 13:10:37 von Tad J McClellan

vorticitywolfe@gmail.com wrote:
> Alright, this is confusing me:
>
> $string = 2340Z 4SL -ABCD PS T1203045;


perl -e '$string = 2340Z 4SL -ABCD PS T1203045;'
Bareword found where operator expected at -e line 1, near "2340Z"
(Missing operator before Z?)
Number found where operator expected at -e line 1, near "Z 4"
(Do you need to predeclare Z?)
Bareword found where operator expected at -e line 1, near "4SL"
(Missing operator before SL?)
syntax error at -e line 1, near "2340Z "


> if $string =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|PS|PT)*?/g{
> print $1,$2,$3,$4;
> }


perl -e 'if $string =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|PS|PT)*?/g{
> print $1,$2,$3,$4;
> }'
syntax error at -e line 1, near "if $string "
Execution of -e aborted due to compilation errors.


> This works


That is impossible.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: regex question about ?, *, and $1

am 19.12.2007 13:22:11 von vorticitywolfe

On Dec 19, 12:10 pm, Tad J McClellan wrote:
> vorticitywo...@gmail.com wrote:
> > Alright, this is confusing me:
>
> > $string = 2340Z 4SL -ABCD PS T1203045;
>
> perl -e '$string = 2340Z 4SL -ABCD PS T1203045;'
> Bareword found where operator expected at -e line 1, near "2340Z"
> (Missing operator before Z?)
> Number found where operator expected at -e line 1, near "Z 4"
> (Do you need to predeclare Z?)
> Bareword found where operator expected at -e line 1, near "4SL"
> (Missing operator before SL?)
> syntax error at -e line 1, near "2340Z "
>
> > if $string =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|PS|PT)*?/g{
> > print $1,$2,$3,$4;
> > }
>
> perl -e 'if $string =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|PS|PT)*?/g{> print $1,$2,$3,$4;
> > }'
>
> syntax error at -e line 1, near "if $string "
> Execution of -e aborted due to compilation errors.
>
> > This works
>
> That is impossible.
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

correction
$string = "2340Z 4SL -ABCD PS T1203045";

forgot the quotes in the translation

Re: regex question about ?, *, and $1

am 19.12.2007 14:04:21 von Michele Dondi

On Wed, 19 Dec 2007 04:22:11 -0800 (PST), vorticitywolfe@gmail.com
wrote:

>> > Alright, this is confusing me:
>>
>> > $string = 2340Z 4SL -ABCD PS T1203045;
[snip]

>> --
>> Tad McClellan
>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Alright: you did good not to top post, but no need to quote the entire
message you're replying to either. Not certainly the .sig, unless you
want to comment on it.

>correction
>$string = "2340Z 4SL -ABCD PS T1203045";
>
>forgot the quotes in the translation

There's a lesson in this: don't retype. Copy and paste instead. It's
also easier, ain't it?


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: regex question about ?, *, and $1

am 19.12.2007 15:02:32 von Paul Lalli

On Dec 19, 5:54 am, vorticitywo...@gmail.com wrote:
> Alright, this is confusing me:
>
> $string = 2340Z 4SL -ABCD PS T1203045;
>
> if $string =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|PS|PT)*?/g{
> print $1,$2,$3,$4;
>
> }
>
> This works like I want it returns values for $1,$2,$3 and $4

I completely fail to believe you.

$ perl -le'
if ("2340Z 4SL -ABCD PS T1203045" =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|
PS|PT)*?/g) {
print qq{1: "$1", 2: "$2", 3: "$3", 4: "$4"};
}
'
1: "", 2: "", 3: "", 4: ""

This is perl, v5.8.8 built for cygwin-thread-multi-64int

Paul Lalli

Re: regex question about ?, *, and $1

am 19.12.2007 15:19:05 von vorticitywolfe

Ok,

After carefully checking again, it works if the "*?" at the end is
removed. That is why it is confusing. I guess that wasn't clear. I
don't understand why the "*?" makes it not return the values for
$1,$2, etc. If it is in there, it returns something, but the values
are blank or undefined. So that makes me think it is matching it...

Re: regex question about ?, *, and $1

am 19.12.2007 15:38:13 von Peter Makholm

vorticitywolfe@gmail.com writes:

> After carefully checking again, it works if the "*?" at the end is
> removed. That is why it is confusing. I guess that wasn't clear. I
> don't understand why the "*?" makes it not return the values for
> $1,$2, etc. If it is in there, it returns something, but the values
> are blank or undefined. So that makes me think it is matching it...

Yes, because in you original you try to match Zero or more instances
of something, followed by zero or more instances of something else,
followed by zero or more instances of yet another thing, and so on.

So what happens is that perl starts at the beginning of you string and
says: Yes I got zero instances of (-|+), I got zero instances on
(AB|YZ), I got zero instances of (EF|CD), I got zero spaces and, finaly
I got zero instances of (PL|PS|PT). So I got a match.

Removing the ending '*?' makes perl actually looking for something
such that it doesn't match the empty substring at the beginning of you
string.

//Makholm

Re: regex question about ?, *, and $1

am 19.12.2007 15:44:44 von vorticitywolfe

Thank you Makholm

That answered my question perfectly! THANKS!

Re: regex question about ?, *, and $1

am 20.12.2007 00:07:35 von rvtol+news

vorticitywolfe@gmail.com schreef:

> if $string =~ m/(-|\+)*(AB|YZ)*(EF|CD)*\s??(PL|PS|PT)*?/g{
> print $1,$2,$3,$4;
> }
>
> $string = 2340Z 4SL PS T1203045;
>
> It doesn't return anything for $1,$2,$3 or $4
> Why?

I would change things like "(AB|YZ)*" to something like "((?:AB|YZ)?)".

--
Affijn, Ruud

"Gewoon is een tijger."