Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Excluding in Substitution

Reply
Thread Tools

Excluding in Substitution

 
 
Grant Nosbush
Guest
Posts: n/a
 
      11-15-2004
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!
 
Reply With Quote
 
 
 
 
Lukas Mai
Guest
Posts: n/a
 
      11-15-2004
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
--
main(int v,char**c){c?main(atoi(c[--v]),0),
puts("")utchar((v/2&&main(v/2,0),48|v&1));}
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      11-15-2004
Grant Nosbush <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
Lukas Mai
Guest
Posts: n/a
 
      11-15-2004
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;
--
print+74.117.115.116,,qq.\c!..not::.her,Perl=>q$ha cker,$,!($,=$")
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this even possible? (Excluding Subnet from Global NAT) Dylan Cisco 2 10-12-2005 12:00 PM
Excluding a file from VS.Net Project Jon Maz ASP .Net 10 11-19-2004 12:48 PM
Cat5500 with RSM excluding Vlans from routing Yves Cisco 4 10-21-2004 05:27 PM
Excluding a page from ASP.NET forms authentication Frank Rizzo ASP .Net 2 01-12-2004 10:56 PM
Excluding certain files from forms authentication Paul Cook ASP .Net 1 08-07-2003 05:40 PM



Advertisments