LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-29-2005, 04:12 PM   #1
scuffell
Member
 
Registered: Jun 2004
Location: Reading, UK
Distribution: SUSE 9.1, SUSE 9.2, SUSE 9.3, Knoppix 3.8, Gentoo 2005.0, cygwn, colinux
Posts: 100

Rep: Reputation: 15
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
 
Old 04-29-2005, 04:50 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
use an intermediate character... change + for =, - for + and = for -
 
Old 04-29-2005, 04:55 PM   #3
scuffell
Member
 
Registered: Jun 2004
Location: Reading, UK
Distribution: SUSE 9.1, SUSE 9.2, SUSE 9.3, Knoppix 3.8, Gentoo 2005.0, cygwn, colinux
Posts: 100

Original Poster
Rep: Reputation: 15
Thanks a lot!
 
Old 04-29-2005, 04:58 PM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
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
 
Old 04-30-2005, 04:35 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
hmm. yeah that would make more sense huh....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
regexp help cliff76 Linux - Newbie 3 03-07-2008 03:15 PM
little help for regexp EmOuBi Linux - Newbie 6 08-06-2005 03:19 AM
regexp help ... pld Programming 1 03-15-2005 04:45 PM
postfix regexp question wijnands Linux - Newbie 1 06-03-2004 07:19 AM
regexp question rytrom Linux - Newbie 3 09-01-2003 01:50 PM


All times are GMT -5. The time now is 11:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration