Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with regular expression 4

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
SI
Hi all,

I've been trying to figure this one out but can't seem to get it. Here's the situation:

I have some text like this:

some text1,some text2,some text3
{
some text1,some text2,some text3
}

What I want to do is add space after the comma (,) OUTSIDE the { }

So the above text should become:

some text1, some text2, some text3
{
some text1,some text2,some text3
}

I just can't seem to get it working. It either puts a space after all commas (also within the { }) or none at all.

Note that it can be zero to many commas outside and inside the {}

Any ideas?

Thanks in advance!
 
Well, I got this combination of regexp and some other code working (at the end $a is what I want), but I was wondering if there is a more fancy way of doing it ;-)

CODE:

$a="
some text1,some text2,some text3
{
some text1,some text2,some text3
}
";

$b=preg_replace("/[^{]*(\{[^}]*\})/","###HERE###$1",$a);
$c=preg_replace("/([^{]*)\{[^}]*\}/","$1#S#",$a);
$tochange=explode('#S#',$c);

foreach ($tochange as $k=>$v) {
$b=preg_replace("/###HERE###/",str_replace(',',', ',$v),$b,1);
}

$a=$b;


Thanks,

c4
 
c4n,

Can you split the string and test the lines like:

Code:
$str = <<<END
some text1,some text2,some text3A
{
some text1,some text2,some text3
}
some text1,some text2,some text3B
{
some text1,some text2,some text3
}
END;

$toggle_r = true;
$lineArray = split("\n", $str);
foreach($lineArray as $line)
{
 if($toggle_r) 
 $line = str_replace(",", ", ", $line);
 
 if((strpos($line, "{") > -1) || strpos($line, "}") > -1)
 $toggle_r = (!$toggle_r);
 
 echo $line."<br />";
}


-Mark
 
Hi Mark,

Thanks for posting.

Unfortunately I can't use that code because it isn't necessary the initial string ($str in your case) will be nicely formatted. It can be

Code:
$str = <<<END
some text1,some text2,some text3A{some text1,some text2,some text3}
some text1,some text2,some text3B{some text1,some text2,some text3}
END;

OR

Code:
$str = <<<END
some text1,some text2,some text3A{
some text1,some text2,some text3
}
some text1,some text2,some text3B{
some text1,some text2,some text3
}
END;

OR

Code:
$str = <<<END
some text1,some text2,some text3A
{some text1,some text2,some text3}
some text1,some text2,some text3B
{some text1,some text2,some text3}
END;

And so on.

I want to output the string in the same "shape" as it was input, so formatting in in the line-by-line format is not an option. I just want to have the space added after the commas located outside { }

My code does the trick, but I'm sure there must be some more elegant and efficient way.

Thank,

c4
 
c4,

Did I mention that I'm one of the fastest RE authors around.
HeeeHaaaw.

And certainly I would appreciate advice on improvements here.

I tested it with all the flavors above and couldn't crash it.
Thanks to Dr J. for the tips.

Code:
$tmp = preg_replace("/([^\{]*)([\{]?)([^\}]*)([\}]?)/e",
	   	"str_replace(',', ', ', '\\1').'\\2\\3\\4'",
		   $str);
					
echo "<pre>".$tmp."</pre>";


I did learn alot from the exercise, don't feel obligated to post back if you are busy.

-Mark
 
try this too:
$tmp = preg_replace("/([^\{]+)(\{[^\}]+\})/e",
"str_replace(',', ', ', '\\1').'\\2'",
$str);

echo "<pre>".$tmp."</pre>";


Known is handfull, Unknown is worldfull
 
Here's a bit more compact version:
Code:
<?php
# 
#
#
$str = <<<END
some text1,some text2,some text3A{
some text1,some text2,some text3
}
some text1,some text2,some text3B{
some text1,some text2,some text3
}
END;

$pattern = "/([^\{]*)(\{.*\})/Ues";
preg_match($pattern,$str,$myArr);
print_r($myArr);
//exit;

$tmp = preg_replace($pattern,"str_replace(',', ', ', '\\1').'\\2'",$str);
                    
echo "<pre>".$tmp."</pre>";

$str = <<<END
some text1,some text2,some text3A{some text1,some text2,some text3}
some text1,some text2,some text3B{some text1,some text2,some text3}
END;
preg_match($pattern,$str,$myArr);
print_r($myArr);

$tmp = preg_replace($pattern,"str_replace(',', ', ', '\\1').'\\2'",$str);
                    
echo "<pre>".$tmp."</pre>";

$str = <<<END
some text1,some text2,some text3A
{some text1,some text2,some text3}
some text1,some text2,some text3B
{some text1,some text2,some text3}
END;
preg_match($pattern,$str,$myArr);
print_r($myArr);

$tmp = preg_replace($pattern,"str_replace(',', ', ', '\\1').'\\2'",$str);
                    
echo "<pre>".$tmp."</pre>";

?>

The pattern:
/([^\{]*)(\{.*\})/Ues

Changes
1. added the U modifier which makes the expression not greedy
2. added s modifier for including newline characters in the scope of the dot wildcard
3. eliminated the character classes for the curly braces and pulled the subexpression into only one entity
Code:
Full explanation:
/     opening regex delimiter
(     begin subpattern
[     open character class definition
^     not - negates the chars listed in the class
\{    the escaped literal opening curly brace
      needs escaping because it has meta meaning in PCRE
]     close char class
*     zero or more
)     close subpattern
(     open subpattern
\{    escaped curly brace
.     anything - wait for the modifier to learn meaning
*     zero or more
\}    escaped closing curly brace
)     close subpattern
/     closing regex delimiter
U     non greedy
e     interpret replacement as PHP code
s     interpret . (dot) including newline chars
 
Thank you both for the better versions.

As a person who started with Perl and is now moving to PHP. (Because it's easier to use with MySQL IMHO)

I thought I would be giving up some of the RE/string handling functionality.

!TRUE
 
Hi all,

Thanks to all for posting this, much appreaciated. I will dig through your codes and I am sure I will learn new things abour RE.

Thanks again!

Regards,

c4
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top