Excluding in Substitution

G

Grant Nosbush

How would I go about removing spaces from a string in perl except
those spaces found in brackets? In other words, if I have the
following string:

$str = "Hello World (My name is Bob) What is your name?"

I would like it to become:

$str = "HelloWorld(My name is Bob)Whatisyourname?"

I was thinking of using substituion in Perl, but not sure what
parameters to use. Anyone have any ideas? Thanks for your help!
 
L

Lukas Mai

Grant Nosbush schrob:
How would I go about removing spaces from a string in perl except
those spaces found in brackets? In other words, if I have the
following string:
[...]

perldoc -q except

HTH, Lukas
 
A

Anno Siegel

Grant Nosbush said:
How would I go about removing spaces from a string in perl except
those spaces found in brackets? In other words, if I have the
following string:

$str = "Hello World (My name is Bob) What is your name?"

I would like it to become:

$str = "HelloWorld(My name is Bob)Whatisyourname?"

Split the string on the parenthesized pieces, capturing the
delimiters. Go through the partial strings and delete spaces in
strings that don't begin with "(". Join everything together again.

$_ = 'Hello World (My name is Bob) What is your name?';
$str = join '', map { tr/ //d unless /^\(/; $_} split /(\([^)]*\))/;

Anno
 
L

Lukas Mai

Grant Nosbush schrob:
How would I go about removing spaces from a string in perl except
those spaces found in brackets? In other words, if I have the
following string:
$str = "Hello World (My name is Bob) What is your name?"
I would like it to become:
$str = "HelloWorld(My name is Bob)Whatisyourname?"
I was thinking of using substituion in Perl, but not sure what
parameters to use. Anyone have any ideas? Thanks for your help!

1 while $str =~ s/^([^(\s]*(?:\([^)]*\)[^(\s]*)*)\s+/$1/;

1 while s/
^ # beginning of string
( # capture
# this is what we want to keep:
[^(\s]* # 0 or more non-whitespace non-( chars
(?: # a group of:
\( # opening (
[^)]* # 0 or more non-) chars
\) # closing )
[^(\s]* # 0 or more non-whitespace non-( chars
)* # ... repeated 0 or more times
) # end of capture
\s+ # 1 or more whitespace chars
# note: this is the first run of whitespace not surrounded by ( )
/
$1 # replace all of the above with the first capturing group, which
# contains only non-whitespace and (...) groups
/xe;
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top