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 11-14-2007, 01:21 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
need quick regex help with substitution


I have a little script that kind of publishes my dev php files to live versions. All my dev files look like "index.dev.php" & the live version looks like "index.php". The script strips out the .dev from references inside the file (includes, hrefs, forms) when it publishes & does this with a sed.

The problem is, it's replacing a couple extra ".dev"'s in the file that I'd rather it not, so I'm looking for a regex that replaces:

[a-z].dev.php with [a-z].php

...meaning that it only strips .dev if it is preceded by a letter. The problem I have is that it's also replacing that preceding letter. How do you carry over a character from the find to the replace? My current regex looks like:

sed 's/[a-z]\.dev\.php"/\.php"/g'

but that makes the line:

include "foo.dev.php";

become

include "fo.php";

when I need it to be

include "foo.php";

ideas?
 
Old 11-14-2007, 01:40 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I'm not 100% positive, but try the following:
Code:
sed 's/\([a-z]\)\.dev\.php"/\1\.php"/g'
Not sure if you want the "'s either.

Code:
sed 's/\([a-z]\)\.dev\.php/\1\.php/g'

Last edited by forrestt; 11-14-2007 at 01:43 PM. Reason: Forgot to escape the parins
 
Old 11-14-2007, 01:42 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You need to use a placeholder:
Code:
sed 's/\([a-z]\)\.dev\.php"/\1\.php"/g'
ta0kira

PS I find sed a lot easier to use with -r. That lets you use () without \, plus a few other things.

Last edited by ta0kira; 11-14-2007 at 01:43 PM.
 
Old 11-14-2007, 01:58 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by ta0kira View Post
You need to use a placeholder:
Code:
sed 's/\([a-z]\)\.dev\.php"/\1\.php"/g'
ta0kira

PS I find sed a lot easier to use with -r. That lets you use () without \, plus a few other things.
fantastic. Thanks!
 
  


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
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
substitution ovince Programming 3 04-28-2007 05:35 AM
Substitution in Perl rigel_kent Programming 4 06-02-2006 10:11 AM
substitution with a function rigel_kent Programming 4 05-20-2006 05:28 PM
Quick regex problem, can't find solution R00ts Programming 3 05-25-2005 02:55 PM

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

All times are GMT -5. The time now is 03:49 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