Welcome Guest, Not a member yet? Register   Sign In
Regular Expression, need help
#1

[eluser]gungbao[/eluser]
Hi there,

stucking in timeline and regex-world, I kindly ask you for some help:

Have PHP-String:
$s = "my name is {constant1} any I am looking for {constant2}";

Want Regex to retrieve:
Array('constant1', 'constant2')


preg_match("/\{(.*)\}/",$s, $captures);
give silly answers.

Could pls. someone help me.
Great thanks in advance,

Cheers Chris
#2

[eluser]pistolPete[/eluser]
Try this:
Code:
// you probably have a typo there '... is {constant1} and ...'
$subject = 'my name is {constant1} any I am looking for {constant2}';
$pattern = '#my name is (.+) any I am looking for (.+)#is';
$result = preg_match($pattern, $subject, $subpattern);
echo '<pre>'.print_r($subpattern, TRUE).'</pre>';
#3

[eluser]gungbao[/eluser]
Sorry, I did not ask that correctly:


{constant1}, {constant2}, ... {constantN} are ment as placeholders in ANY string content, similar to a simplified template language. I am looking to grep all that tokens within the {...} with a regular expr.


Hope its clarified better, now Smile
Greetz
Chris
#4

[eluser]pistolPete[/eluser]
Okay, try this:

Code:
$subject = 'my name is {constant1} any I am looking for {constant2}';
$pattern = '#\{(.+)\}#isU';
$result = preg_match_all($pattern, $subject, $subpattern);
echo '<pre>'.print_r($subpattern[1], TRUE).'</pre>';
#5

[eluser]gungbao[/eluser]
Works finest! PistolPete, once again - very great help from your side!
#6

[eluser]BrianDHall[/eluser]
Regex is very powerful yet can be such a pain. For a really great reference site check out: http://www.regular-expressions.info/

Also I highly recommend having an editor that allows regex search/replace functions, as it is so much easier to paste example text into a text file and test inside the editor than it is to try running it through PHP first.




Theme © iAndrew 2016 - Forum software by © MyBB