Computing.Net > Forums > Programming > need help with regexp

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

need help with regexp

Reply to Message Icon

Name: Justin
Date: July 14, 2003 at 13:12:46 Pacific
OS: Solaris 8
CPU/Ram: Ultra SPARC 1 gig
Comment:

I am a beginner programmer and I've got thousands of lines of code that need to be swapped. I've been working on it by hand for 3 days and I think I'm starting to go blind. ;-P I was wondering if there was any way that I can do this with a regular expression with the sed command or in emacs with regexp or even a shell script.

I want to take this...
mapping[ $variable1 ] = main::$variable2;
mapping[ $variable3 ] = main::$variable4;

and make this...
mapping[ main::$variable2 ] = $variable1;
mapping[ main::$variable3 ] = $variable4;

This would be much easier to do with a regular expression if all of the variables were the same name. Unfortunately thats not the case. I'm not asking for anyone to do my job for me, but perhaps someone could point me in the right direction as how to swap these variables in the same line, or if it can even be done automated at all.

Thanks in advance,
Justin



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: July 14, 2003 at 21:04:15 Pacific
Reply:

I can't help with putting it into a batch script or anything, but I'll give it a go. I know regular expressions in a perl context, and I think you've asked perl questions before so I think you've got it available.

You'll note that your example is inconsistent...variables 1 and 2 swap, but 3 and 4 don't...I'll assume this is a typo and they should be swapped.

Assuming a line of code, or all of the code is in a scalar $mycode, the following line would do it:

$mycode =~ s/mapping\[ (.+?) ] = (.+?);/mapping[ $2 ] = $1\;/gm;

Note that perl assigns the variables $1,$2,,,$n to the nth set of parenthesis in a regexp. This is what makes this possible. I don't know if this behavior is duplicated in batch scripts, emacs, or anything else.

Learn everything you ever wanted to know about perl regular expressions here. You'll find pertinent information in the "search and replace" section.

Finally, you have a history of not posting back and letting people know if what they suggested worked or not. Could you start doing this so I know if this stuff helps? Thanks.

Good luck,
-SN


0

Response Number 2
Name: SN
Date: July 14, 2003 at 21:07:20 Pacific
Reply:

Whoops didn't mean to escape the ; in that regexp.

$mycode =~ s/mapping\[ (.+?) ] = (.+?);/mapping[ $2 ] = $1;/gm; 
-SN


0

Response Number 3
Name: Justin
Date: July 15, 2003 at 10:49:10 Pacific
Reply:

THanks for your help SN. It worked perfectly. I appreciate your help. I will try to post back results more often.

Justin


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: need help with regexp

Need help with WinXP batch script www.computing.net/answers/programming/need-help-with-winxp-batch-script/13165.html

Need help with an SQL join query www.computing.net/answers/programming/need-help-with-an-sql-join-query/13358.html

Need help with algorithm!! www.computing.net/answers/programming/need-help-with-algorithm/594.html