LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 05-02-2008, 03:52 PM   #1
chadwick
Member
 
Registered: Apr 2005
Location: At the 100th Meridian where the great plains begin
Distribution: Debian Testing on T60 laptop
Posts: 105

Rep: Reputation: 17
perl: how to insert numerical digit immediately after regexp backreference variable?


I'm trying to insert the digit 0 (zero) into a string, as the following.
Convert:

xxxxxxx-d-xxxxxx.mp3

to

xxxxxxx-0d-xxxxxx.mp3

where d is a decimal digit and x means I don't care what it is.

If I do it like:

s/(.*-)(\d)(-.*mp3)/$1 0$2$3/;

then it prints a space where I have a space.

If I do it like:

s/(.*-)(\d)(-.*mp3)/$10$2$3/;

then it doesn't end up using $1, possibly since it thinks I'm trying to use $10 which doesn't exist.

If I do it like:

s/(.*-)(\d)(-.*mp3)/{$1}0$2$3/;
s/(.*-)(\d)(-.*mp3)/($1)0$2$3/;
s/(.*-)(\d)(-.*mp3)/$1{0}$2$3/;
s/(.*-)(\d)(-.*mp3)/$1(0)$2$3/;

Then it prints the brackets which I don't want.
I've tried using named backreferences instead but I can't get that to work. Not sure if I'm using the wrong syntax or the wrong version of perl. I'm using perl v5.8.8

Thanks
 
Old 05-02-2008, 04:03 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Two ways to do it (I'm sure there are others, and probably better):

Code:
$zero='0';
$file =~ s/(.*-)(\d)(-.*mp3)/$1$zero$2$3/;
or
Code:
$file =~ s/(.*-)(\d)(-.*mp3)/$1 $2$3/;
$file =~ s/ /0/;
HTH

Forrest
 
Old 05-02-2008, 04:05 PM   #3
nifund9
Member
 
Registered: Oct 2004
Distribution: Slackware 12.1, FreeBSD 7.0, UbuntuStudio 8.10
Posts: 32

Rep: Reputation: 15
My Perl is a little rusty, but is it:

s/(.*-)(\d)(-.*mp3)/$1\0$2$3/;
 
Old 05-02-2008, 04:17 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
One more if you want to have it all on one line:
the "-" is just to have a character, and the \b deletes it:

Code:
s/(.*-)(\d)(-.*mp3)/$1-\b0$2$3/;
HTH

Forrest
 
Old 05-02-2008, 04:19 PM   #5
chadwick
Member
 
Registered: Apr 2005
Location: At the 100th Meridian where the great plains begin
Distribution: Debian Testing on T60 laptop
Posts: 105

Original Poster
Rep: Reputation: 17
Thanks. I went with the $zero='0' method which worked really well.
I tried the \0 method as well but that didn't seem to do it.
 
Old 05-02-2008, 04:23 PM   #6
chadwick
Member
 
Registered: Apr 2005
Location: At the 100th Meridian where the great plains begin
Distribution: Debian Testing on T60 laptop
Posts: 105

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by forrestt View Post
the "-" is just to have a character, and the \b deletes it:
Nice, I'd never seen that before. I'll remember it for next time.
 
Old 05-18-2008, 10:13 PM   #7
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by nifund9 View Post
My Perl is a little rusty, but is it:

s/(.*-)(\d)(-.*mp3)/$1\0$2$3/;
One more way to do it, this being Perl, after all:

Code:
 s/(.*-)(\d)(-.*mp3)/${1}0$2$3/;
I haven't actually tested this, but it's the way that you would handle a variable outside of a regular expression, eg.

Code:
$foo = "ASDF";
$foobar = "You Lose";
print "${foo}bar";  # prints "ASDFbar"
print "$foobar";    # prints "You Lose"
 
Old 05-18-2008, 11:18 PM   #8
chadwick
Member
 
Registered: Apr 2005
Location: At the 100th Meridian where the great plains begin
Distribution: Debian Testing on T60 laptop
Posts: 105

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by bartonski View Post
One more way to do it, this being Perl, after all:

Code:
 s/(.*-)(\d)(-.*mp3)/${1}0$2$3/;
I haven't actually tested this, but it's the way that you would handle a variable outside of a regular expression, eg. ...
Yes this makes sense. It's what I was trying to do with my brackets above but couldn't remember how to do it properly and ended up thinking that I was making stuff up. Thanks for pointing that out.
 
Old 05-19-2008, 12:49 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Btw, you can avoid the issue with 0-width assertions (which are an oft-overlooked part of Perl’s regular expressions). For example,
Code:
s/(?<=-)(?=\d-.*mp3)/0/
 
  


Reply



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
Need a perl regexp master sal_paradise42 Programming 8 10-14-2007 06:17 PM
Perl Regexp search-n-replace jpbarto Programming 2 06-16-2005 12:45 PM
Perl/regexp help... - query string parsing... lowpro2k3 Programming 4 05-11-2005 05:18 PM
perl simple regexp champ Programming 3 07-07-2004 03:27 AM
perl regexp problem raven Programming 4 03-21-2004 11:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 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
Open Source Consulting | Domain Registration