473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Regex Help

Wow, it's been a long time since I've had to ask for that, but this has me
stumped. It's probably going to be something simple I'm overlooking, in
which case I shall properly beat myself in the forehead with a board while
chanting sonorously, but meanwhile...

I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = '|||||';
$x = preg_replace('=\| *\|=U', '|_|', $x);

Result:

|_||_||

What I need is:

|_|_|_|_|

Any suggestions?

--
Alan Little
Phorm PHP Form Processor
http://www.php-form.net/
Jun 2 '08 #1
6 1113
Alan Little escribió:
I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = '|||||';
$x = preg_replace('=\| *\|=U', '|_|', $x);

Result:

|_||_||

What I need is:

|_|_|_|_|
Ugly and not fully tested, but I hope it can give you an idea:

<?php

$x = '|||||';

$substrings = explode('|', $x);
foreach($substrings as &$i){
if( preg_match('/^\s*$/i', $i) ){
$i = '_';
}
}

echo substr(implode('|', $substrings), 1, -1);

?>


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 2 '08 #2
On Thu, 17 Apr 2008 17:50:04 +0200, Alan Little
<al**@n-o-s-p-a-m-php-form.netwrote:
Wow, it's been a long time since I've had to ask for that, but this has
me
stumped. It's probably going to be something simple I'm overlooking, in
which case I shall properly beat myself in the forehead with a board
while
chanting sonorously, but meanwhile...

I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing
or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = '|||||';
$x = preg_replace('=\| *\|=U', '|_|', $x);

Result:

|_||_||
so it finds 1 & 3, not 2 & 4.
What I need is:

|_|_|_|_|

Any suggestions?
<?php
$x = '|||||';
$x = preg_replace('/\|(\s*)(?=\|)/', '|_', $x);
echo $x;
?>
--
Rik Wasmus
Jun 2 '08 #3
On Thu, 17 Apr 2008 10:50:04 -0500, Alan Little
<al**@n-o-s-p-a-m-php-form.netwrote in
<Xn**************************@216.196.97.131>:
>Wow, it's been a long time since I've had to ask for that, but this has me
stumped. It's probably going to be something simple I'm overlooking, in
which case I shall properly beat myself in the forehead with a board while
chanting sonorously, but meanwhile...

I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have nothing or
only spaces between them. However, if I have consecutive occurrences of
this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:

$x = '|||||';
$x = preg_replace('=\| *\|=U', '|_|', $x);
Are you sure that PRCE is enabled (to make the 'U' affect the
pattern's greediness)?
>Result:

|_||_||

What I need is:

|_|_|_|_|

Any suggestions?
This is less elegant, but should work:

$x = '||||||';
$x = preg_replace('/\|/', '|_', $x);
$x = substr($x, 0, strlen($x) - 1);

With something this simple, you could also use str_replace. Of course
if you've wildly simplified the code for demo purposes, this
suggestion may not be viable. I mention it because sometimes the
simplest solution eludes us. :)
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Jun 2 '08 #4
Carved in mystic runes upon the very living rock, the last words of Rik
Wasmus of comp.lang.php make plain:
On Thu, 17 Apr 2008 17:50:04 +0200, Alan Little
<al**@n-o-s-p-a-m-php-form.netwrote:
>I have a string broken into sections using the vertical pipe as a
delimiter. I want to insert an underscore between any which have
nothing or only spaces between them. However, if I have consecutive
occurrences of this pattern, it only finds 1 and 2, but not 2 and 3.
To illustrate:

$x = '|||||';
$x = preg_replace('=\| *\|=U', '|_|', $x);

Result:

|_||_||

so it finds 1 & 3, not 2 & 4.
>What I need is:

|_|_|_|_|

Any suggestions?

<?php
$x = '|||||';
$x = preg_replace('/\|(\s*)(?=\|)/', '|_', $x);
echo $x;
?>
Thanks, that works. Now I'll have to sit down and figure out why. :)

--
Alan Little
Phorm PHP Form Processor
http://www.php-form.net/
Jun 2 '08 #5
Carved in mystic runes upon the very living rock, the last words of
Charles Calvert of comp.lang.php make plain:
This is less elegant, but should work:

$x = '||||||';
$x = preg_replace('/\|/', '|_', $x);
$x = substr($x, 0, strlen($x) - 1);

With something this simple, you could also use str_replace. Of course
if you've wildly simplified the code for demo purposes, this
suggestion may not be viable. I mention it because sometimes the
simplest solution eludes us. :)
Thanks, but I need it to replace only zero or more spaces between the bars
with the underscore. A better example:

$x = '| | abc | | x||';

Should yield:

|_| abc |_| x|_|

Rik's pattern does it; I've just never learned about assertions.

--
Alan Little
Phorm PHP Form Processor
http://www.php-form.net/
Jun 2 '08 #6
*** Alan Little escribió/wrote (Thu, 17 Apr 2008 13:57:45 -0500):
><?php
$x = '|||||';
$x = preg_replace('/\|(\s*)(?=\|)/', '|_', $x);
echo $x;
?>

Thanks, that works. Now I'll have to sit down and figure out why. :)
The (?=....) part is a lookahead assertion. From manual:

"An assertion is a test on the characters following or preceding the
current matching point that does not actually consume any characters"

I admit I had never dived so deep in the Pattern Syntax manual page <:-)
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Jun 2 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
7
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim...
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.