View Single Post
  #6 (permalink)  
Old March 8th, 2007, 09:12 AM
ciderpunx ciderpunx is offline
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

So are you after something that looks for anything in the string that is in brackets and then removes the '-' from it? You really don't need to match and then run the substitution. What you're doing at the moment is saying:

- Take a string
- See if it matches a pattern
- If it does match the pattern, match the whole string for a different pattern and substitute (in this case delete) it.

So for the string "I hope we-will-find (a-Solution)"

if($str =~ m/(\()($word+(\-$word+){1,}$word+)/(\))) {
   $str =~ s/\-//;
Will do exactly the same as this:
$str =~ s/\-//;
Because $str is always $str until you assign something else to it, or try and change it.

You could rewrite your example as:

$str="I hope we-will-find (a-Solution)"; # $str=="I hope we-will-find (a-Solution)"
$str=~s/(\(\w+)\-(\w+\))/$1 $2/;         # $str=="I hope we-will-find (a Solution)"
                                         # nb \w is a shortcut for [a-zA-Z_]
print "Got: '$str'";
Charlie Harvey's website - linux, perl, java, anarchism and punk rock:
Reply With Quote