In our last episode,
the lovely and talented bingster
broadcast on comp.lang.perl.misc:
If there is a string like this:
$test = 'a bc (B, M, D),d e (B, M),lfm (D)'
how can I remove all the '(*.)' parts to make it something like:
This doesn't do what you think. I think you have got *. where you
meant .*, but even correcting that won't do what you think.
As you have written it above, you are looking to match zero or more
(s followed by any single character, followed by ). This will remove
the (D) at the end of your string and all the )s with the character
that precedes them.
But the result is 'a bc (B, M, ,d e (B, ,lfm '.
Exactly.
Now I don't know whether when you wrote *. it was a typo for .* or
whether you are really confused about what * and . mean. But let's
try it the other way, in case it was a typo.
You may have meant:
$test =~ s/\(.*\)//g;
which says match on ( followed by zero or more of any character
followed by ).
But the result in this case would be 'a bc '. You see REGULAR
EXPRESSIONS ARE GREEDY (write this in stone), which means they
will match the biggest string they can. And the biggest match
here begins with the first ( and ends with the last ). But that
is not what you want. You want to match the first ( and everything
up to and including the first ), and then you want to match the
second ( and everything up to and including the second ) and so
forth.
So try this:
$test =~ s/\([^)]*\)//g;
This says, match a ( followed by zero or more characters that
are not ) and then a ). Notice that you do not escape the ) in
the square brackets because ) is not special in square brackets
- the characters that are special in square brackets are -]\^$
..
This gives you:
a bc ,d e ,lfm
which isn't quite what you want because you want the leading space
with (s out too, if there is one, but it is a step in thr right direction.
In order to remove that white space character if there is one, this
will work (you may want to adjust it if you have more than one white
space character or if you really only want to remove space characters
and not any white space character):
$test =~ s/\s?\([^)]*\)//g;
This gives you:
a bc,d e,lfm
which is exactly what you asked for:
I believe there are other ways to make regular expressions less
greedy, and perhaps some of them are better, but this makes sense
to me.