On 3 Sep 2007 at 17:44, Rob Dixon wrote:
> Beginner wrote:
> >
> > I am trying to come up with a regex to squash multiple commas into
> > one. The line I am working on looks like this:
> >
> > SPEED OF LIGHT, , LIGHT SPEED,TRAVEL,TRAVELLING, ,
> > DANGER,DANGEROUS,PHYSICAL, , CONCEPT,CONCEPTS, , , , , , , , , ,
> >
> > There are instances of /,\s{1,},/ and /,,/
> >
> > The bit that I am struggling with is finding a way to get a use a
> > multiplier for the regex /,\s+/ but I have to be careful not to
> > remove single entries. I guess the order of my substitutions is
> > important here.
> >
> > Can anyone offer any tips please?
>
> Hey Dermot.
>
> I think just
>
> $text =~ s/,[,\s]+/,/g;
>
Indeed Rob that works too.
You've used square brackets for what I think they call 'alternation';
the next character might be a comma and a whitespace. I have always
thought of square brackets as being for character classes EG: [a-z].
I associate alternation with parenthesis and the pipe /(this|that)/
perlrequick demos examples like:
/[a-z]+\s+\d*/; # match a lowercase word, at least some space, and
# any number of digits
but I don't think I've seen examples where there is a character class
like \s or \w within square brackets before.
Anyway back to reading perlretut, perlop and others.
Thanx,
Dp.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/