LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-06-2004, 01:09 PM   #1
amytys
LQ Newbie
 
Registered: Sep 2004
Posts: 11

Rep: Reputation: 0
Exclamation regular expression search/replace question (HELP!!! :)


Given the code:

$sDemo = "______";

I want to replace EACH instance of "__" (two underscores) with "_ _" (space in between).

I tried the code:

$sDemo =~ s/__/_ _/g;

Unfortunately, this changes the first instance, but doesn't go on to ask the question if the second "_" in the first pair was also followed by an underscore, and change that, and so on.

So, given seven underscores, "_______", I get "_ __ __ __" instead of what I want, which is "_ _ _ _ _ _ _".

What should my regular expression look like to do what I want?

Also, what if the delimiter that I'm looking for is a pipe? I tried prefixing it with a \, but that didn't seem to work. How does one format the search or replace side to work with pipes as the search/replace char?

Thanks a lot... looks like a GREAT forum!
 
Old 09-06-2004, 01:18 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
This:

I want to replace EACH instance of "__" (two underscores) with "_ _" (space in between). (dash space dash dash space dash ......)

and this:

of what I want, which is "_ _ _ _ _ _ _" (dash space dash space ........)

is not the same.

If you want the second (which I think you do ):

echo "______" | sed 's/__/_ /g'

Hope this helps.
 
Old 09-06-2004, 01:25 PM   #3
amytys
LQ Newbie
 
Registered: Sep 2004
Posts: 11

Original Poster
Rep: Reputation: 0
Nope, that doesn't do it.

The proposal,
echo "______" | sed 's/__/_ /g'

would result in "_ _ _ _"

what I would want is "_ _ _ _ _ _ _"

See, the number of underscores in the string doesn't change... I just want to split it up so that I've got a space in between each underscore.
 
Old 09-06-2004, 01:35 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
That's a typo of mine,sorry:

echo "______" | sed 's/_/_ /g'

There wil be a <space> at the end (not 100% what you want, but cureable....)
 
Old 09-06-2004, 01:53 PM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Try this Perl code :

Code:
$sDemo = "______";
$sDemo =~ s/_{2}/_ _ /g;
chop $sDemo;
print "$sDemo\n";

Last edited by Cedrik; 09-06-2004 at 01:57 PM.
 
Old 09-06-2004, 02:36 PM   #6
amytys
LQ Newbie
 
Registered: Sep 2004
Posts: 11

Original Poster
Rep: Reputation: 0
Yes, thank you. #5 did what I was looking for!
 
  


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
Regular expression question gauge73 Linux - General 5 10-28-2005 11:33 AM
Regular expression question gauge73 Linux - General 2 10-28-2005 09:32 AM
regular expression question zero79 Linux - Software 1 07-11-2005 07:03 PM
Kate - Regular Expression Replace and Other Features? apachedude Linux - Software 0 01-04-2005 01:49 PM
Regular expression question - John Sloan Linux - Software 1 09-08-2004 12:33 PM

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

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