LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Regexp question (https://www.linuxquestions.org/questions/programming-9/regexp-question-318507/)

scuffell 04-29-2005 03:12 PM

Regexp question
 
Hi,

I am using a regular expression in perl.

What I want to do is swap every occurence of + for - and every occurence of - for +.

For example:

I want to change:

2-4+1+6-3

to...

2+4-1-6+3.

Unfortunately, I'm not sure you can do this with 2 regexps, because:

2-4+1+6-3
(change all +s to -s)
2-4-1-6-3
(change all -s to +s)
2+4+1+6+3


Any ideas?

Thanks

acid_kewpie 04-29-2005 03:50 PM

use an intermediate character... change + for =, - for + and = for -

scuffell 04-29-2005 03:55 PM

Thanks a lot!

rjlee 04-29-2005 03:58 PM

Use a translation regexp, not a substitution one:
Code:

my $x='1+2-3+4';
$x =~ tr/+-/-+/;
print "$x\n"

This produces:
Code:

1-2+3-4

acid_kewpie 04-30-2005 03:35 AM

hmm. yeah that would make more sense huh....


All times are GMT -5. The time now is 01:10 AM.