Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 5th, 2007, 05:17 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular Expression in Perl

Hi everyone,
I am a new perl programmer and I have a problem with regular expressions in perl.
I wrote a regular expression in order to match a certain part of a string and then I wanted to be able get and change that part of the string (that is, matched substring), but I don't know how to get the matched substring and change it in perl. (I wrote the regular expression and it is working correctly and doing its job well)
For exampple;
Let's assume our string is, $str = "The men in the room looked at the Men in the street.", and I wrote a reg. expr. that matches /Men/, and I want to substitute -en-(in Men) to -an-. So, if I write the following code;
<code> if($str =~ m/Men/) {
          $str = s/en/an; } </code>
this code will change the -en- that is the first one (in men) in the string which is I don't want.

As a result, my problem is the same, I wrote a reg. expr., it matches a substr. but I am not able to change the content of that substr.

Please help, I know there is a way to make this in perl but I couldn't find it.

Thanks.




Reply With Quote
  #2 (permalink)  
Old March 6th, 2007, 07:35 AM
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

Hey CaNNaC,

You're nearly there. This will do it:

Code:
if ($str=~m/Men/) {
  $str =~ s/en/an/;
}
Cheers

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #3 (permalink)  
Old March 6th, 2007, 09:03 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by ciderpunx
You're nearly there. This will do it:
Code:
if ($str=~m/Men/) {
††$str =~ s/en/an/;
}
Thanks, I know that this will substitute -en- to -an-. This will just change the first -en- in the string to -an-, but I want to change the -en- in the matched reg. expr. In my example (The men in the room looked at the Men in the street), it will change -en- in men to -an-, but as you noticed I matched Men and I didn't change it. So, problem is how to get the content of matched string and change it without changing any other unmatched substring.

Thanks.

Reply With Quote
  #4 (permalink)  
Old March 7th, 2007, 06:32 AM
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

Ah ok

Code:
$str =~ s/(M)en/$1an/;
Or, more simply:

Code:
$str =~ s/Men/Man/;
If you want to change every instance then you can do

Code:
$str =~s/(M)en/$1an/g;
If you only want to change the word Men, and not Mendicant, for example, or HeMen then ad \b (word boundaries). i.e.

Code:
$str=~s/(\b)(M)en(\b)/$1$2an$3/;
This last example gives me

Code:
$ echo The men in the room looked at the Men in the street |  perl -pe 's/(\b)(M)en(\b)/$1$2an$3/g'
The men in the room looked at the Man in the street
$
You may want to downcase the M from Men, so you could do:

Code:
$ echo The men in the room looked at the Men in the street |  perl -pe 's/(\b)(M)en(\b)/$1\l$2an$3/g'
The men in the room looked at the man in the street
$
HTH
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #5 (permalink)  
Old March 7th, 2007, 04:33 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Charlie,

Thanks for your help really. I thought the example I gave will solve my problem, but it didn't :(
In the example I just matched a string that I already know -that is Men-, but here is the problem if I write a reg. expr. instead of Men(again it may match Men but we don't know it), then it will match a string that we cannot predict exactly which word it will be.

For example:
Code:
my $word = "([a-z]|[A-Z])";
my $str = "I hope we-will-find (a-Solution)";

if($str =~ m/(\()($word+(\-$word+){1,}$word+)/(\))) {
   #here as it is seen from the reg. expr. I will match (a-Solution)
   #and I want to delete - in this matched substring without deleting the others
   #It should be (aSolution) after deletion. But we cannot know the substring that will match before it matches
   #as it is in previous example.
   $str =~ s/\-//;
   #if i write this, this will delete the first -, but not in the (a-solution.)
   #So, how can we tell it should delete the one that it matched in reg. expr.
}
Thanks for your help.


Reply With Quote
  #6 (permalink)  
Old March 8th, 2007, 08:12 AM
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
  #7 (permalink)  
Old March 8th, 2007, 02:01 PM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Charlie,

Finally I solved the problem, and everything is really fine and I am comfortable with reg. expressions now.

Thanks.

Reply With Quote
  #8 (permalink)  
Old March 9th, 2007, 05:47 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

s/foo/bar/;
replaces the first occurrence of the exact character sequence foo in the "current string" (in special variable $_) by the character sequence bar; for example, foolish bigfoot would become barlish bigfoot

www.crmpicco.co.uk
www.ie7.com
Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression if-then smay Javascript 4 July 5th, 2005 12:10 PM
regular expression vohra_vikas2004 ADO.NET 3 November 18th, 2004 08:59 PM
Regular expression help!!! alex_read Javascript 4 August 18th, 2004 03:44 AM
regular expression Moharo Pro PHP 3 April 30th, 2004 04:44 AM
perl compatible regular expressions richard.york Pro PHP 5 November 6th, 2003 03:31 PM



All times are GMT -4. The time now is 10:11 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.