LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-27-2006, 10:43 PM   #1
fudam
Member
 
Registered: Feb 2006
Distribution: FC
Posts: 86

Rep: Reputation: 15
Number incrementing using regular expressions


I don't think this is actually possible, but I figured if it was someone here would know how...

I'd like to increment a number within a regular expression. For example it would be cool if I could do something like this, in say Perl:
Code:
$mystring =~ s/_(\d)/$1++/;
So that if $mystring was "Use file_2", it would become "Use file_3"; and if $mystring was "This is example_2_here" it would become "This is example_3_here".

Now, I'm not looking for a multi-line program to do this. I can already do that. I'm wondering if there is a way in Perl, SED, Awk, BASH, or whatever to do this in ONE LINE.

Thanks.
 
Old 11-27-2006, 11:16 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
sure you can. Use the e option which tells perl that the right hand side of the s/left/right/ should be treated as an expression:
Code:
$mystring =~ s/_(\d)/$1+1/e;
 
Old 11-28-2006, 01:48 AM   #3
fudam
Member
 
Registered: Feb 2006
Distribution: FC
Posts: 86

Original Poster
Rep: Reputation: 15
That's awesome... but I'm still a little too stupid to finish this myself. :P How do I include the underscore also? I get an error when I try to do this:
Code:
$mystring =~ s/_(\d)/_$1+1/e;
 
Old 11-28-2006, 01:58 AM   #4
fudam
Member
 
Registered: Feb 2006
Distribution: FC
Posts: 86

Original Poster
Rep: Reputation: 15
Nevermind, I got it. After typing every stupid thing I could think of (couldn't find it on google) I finally got this to work:

Code:
$mystring =~ s/_(\d)/"_" . ($1+1)/e;
Thanks again for your help matthewg42!!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expressions. stomach Linux - Software 1 02-10-2006 07:41 AM
Automaticly incrementing Version Number BoldKiller Programming 7 03-26-2005 01:08 PM
Regular Expressions overbored Linux - Software 3 06-24-2004 03:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 11-01-2003 12:09 AM
Regular expressions aromes Linux - General 1 10-15-2003 01:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:20 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration