Regex Help
am 17.04.2008 17:50:04 von Alan Little
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/
Re: Regex Help
am 17.04.2008 18:32:26 von alvaroNOSPAMTHANKS
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:
$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
--
Re: Regex Help
am 17.04.2008 18:55:20 von luiheidsgoeroe
On Thu, 17 Apr 2008 17:50:04 +0200, Alan Little =
wrote:
> Wow, it's been a long time since I've had to ask for that, but this ha=
s =
> me
> stumped. It's probably going to be something simple I'm overlooking, i=
n
> 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 nothi=
ng =
> or
> only spaces between them. However, if I have consecutive occurrences o=
f
> this pattern, it only finds 1 and 2, but not 2 and 3. To illustrate:
>
> $x =3D '|||||';
> $x =3D preg_replace('=3D\| *\|=3DU', '|_|', $x);
>
> Result:
>
> |_||_||
so it finds 1 & 3, not 2 & 4.
> What I need is:
>
> |_|_|_|_|
>
> Any suggestions?
$x =3D '|||||';
$x =3D preg_replace('/\|(\s*)(?=3D\|)/', '|_', $x);
echo $x;
?>
-- =
Rik Wasmus
Re: Regex Help
am 17.04.2008 18:58:11 von Charles Calvert
On Thu, 17 Apr 2008 10:50:04 -0500, Alan Little
wrote in
:
>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
Re: Regex Help
am 17.04.2008 20:57:45 von Alan Little
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
> wrote:
>
>> 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?
>
>
> $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/
Re: Regex Help
am 17.04.2008 21:01:58 von Alan Little
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/
Re: Regex Help
am 17.04.2008 22:32:46 von webmasterNOSPAMTHANKS
*** Alan Little escribió/wrote (Thu, 17 Apr 2008 13:57:45 -0500):
>>
>> $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
--