regular expression help

T

teds

Hi,

I'm trying to parse out all the letters in an expression like this:

A, B, C

where I won't know ahead of time how many letters I'll have (i.e., it
could be A, B, C, D, E, F ). So I tried this:

$str =~ / (\w*\,\s*)+ \s (\w*) /

on A, B, C

and what I get for $1=B, $2 = C. It seems that $1 is giving me the
last
entry in the match of A, B,. I want to get A as well so I thought I
would make sure I have 1 match of (\w*\,\s*)+. When I do the
following:

$str =~ / ( ( \w*\,\s* )+ ){1} \s (\w*) /

I now get $1 = A, B, but now $2 is B, and $3 is C.

I'm curious why $2 = B, and also wonder if there's a better way of
doing this.

Thanks,

Ted
 
I

it_says_BALLS_on_your forehead

Hi,

I'm trying to parse out all the letters in an expression like this:

A, B, C

where I won't know ahead of time how many letters I'll have (i.e., it
could be A, B, C, D, E, F ).

why don't you just do this?

my $string = 'A, B, C, D, E, F';
$string =~ s/\s//g;
my @letters = split( /,/, $string );

map { print "-$_-\n"; } @letters;
 
P

Paul Lalli

I'm trying to parse out all the letters in an expression like this:

A, B, C

where I won't know ahead of time how many letters I'll have (i.e., it
could be A, B, C, D, E, F ). So I tried this:

$str =~ / (\w*\,\s*)+ \s (\w*) /

on A, B, C

and what I get for $1=B, $2 = C. It seems that $1 is giving me the
last
entry in the match of A, B,. I want to get A as well so I thought I
would make sure I have 1 match of (\w*\,\s*)+. When I do the
following:

$str =~ / ( ( \w*\,\s* )+ ){1} \s (\w*) /

A quantifier of {1} is 100% pointless, in every regular expression
ever. It does absolutely nothing.
I now get $1 = A, B, but now $2 is B, and $3 is C.
I'm curious why $2 = B,

$1 is the entire match of ((\w*,\s*)+). That is, one or more of
(\w*,\s*). So it becomes all of 'A, B, '

$2 is as it was before, the *last* time (\w*,\s*) matches. Nothing
changed from this regexp to the previous one. That's simply what a
parenthesized submatch with a quantifier returns in the $1,$2,$3,etc
variables.
and also wonder if there's a better way of doing this.

I can see two different ways. One uses split, and assumes every letter
is separated by a comma and space. The other uses regexps, and assumes
there are no characters other than the letters you want, plus commas
and spaces.
#!/usr/bin/perl
use strict;
use warnings;

my $str = 'A, B, C, D, E';

my @splits = split /, /, $str;
my @regexps = $str =~ /[A-Z]/g;

print "Splits: @splits\nRegexps: @regexps\n";
__END__
Splits: A B C D E
Regexps: A B C D E


Paul Lalli
 
P

Paul Lalli

it_says_BALLS_on_your forehead said:
why don't you just do this?

my $string = 'A, B, C, D, E, F';
$string =~ s/\s//g;
my @letters = split( /,/, $string );

Why are you doing one operation in two steps? If you want to split on
a comma and space, split on a comma and space:

my @letters = split /, /, $string;
map { print "-$_-\n"; } @letters;

perldoc -q "void context"

If you're using anything prior to 5.8.1, this is a bad idea. Use
instead:

print "-$-\n" for @letters;
 
I

it_says_BALLS_on_your forehead

Paul said:
Why are you doing one operation in two steps? If you want to split on
a comma and space, split on a comma and space:

true, i was unsure if there would always be a space following the
comma, although nothing the OP posted would lead me to believe
otherwise. i just thought this would be more robust.
my @letters = split /, /, $string;


perldoc -q "void context"

If you're using anything prior to 5.8.1, this is a bad idea. Use
instead:

hehe, yeah, i read about that afterwards :)
 
G

Gunnar Hjalmarsson

it_says_BALLS_on_your forehead said:
true, i was unsure if there would always be a space following the
comma, although nothing the OP posted would lead me to believe
otherwise. i just thought this would be more robust.

my @letters = split /\s*,\s*/, $string;
 
E

Eric J. Roode

A quantifier of {1} is 100% pointless, in every regular expression
ever. It does absolutely nothing.

Maybe it should be {1,1} ?

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top