View Single Post
  #6 (permalink)  
Old March 8th, 2007, 08: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
Default

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)"

This
Code:
if($str =~ m/(\()($word+(\-$word+){1,}$word+)/(\))) {
   $str =~ s/\-//;
}
Will do exactly the same as this:
Code:
$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:

Code:
$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: http://charlieharvey.org.uk
Reply With Quote