views:

61

answers:

3

Hi,

I'd like to match and extract variables from: {{variable:int}}

  • variable would be anything a-z
  • : is a separator
  • int would be an integer 0-9

Curretly i have: preg_replace('!\{\{(\S+)\}\}!', "$1", $string) which does only half the job, i still have to split by :.

Thank you!

+1  A: 

Use

{{([a-zA-Z]+):(\d+)}}

$1 will contain the captured variable, $2 will contain the captured integer

Explanation

{{([a-zA-Z])+:(\d+)}}

[a-zA-Z]+ means atleast one more alphabets (small or caps)
followed by a ":"
followed by atleast one or more digits (0-9)

Jass
+2  A: 

You need a non-greedy match (.*?): preg_replace('!\{\{(.*?):(\d)\}\}!')

soulmerge
+1  A: 

If you want to extract the name/value, I think you want to use preg_match.

preg_match('!\{\{(.*?):(\d)\}\}!', $string, $matches);  
$varname = $matches[1];  
$val = $matches[2];
Evän Vrooksövich
Suited my needs best. Thank you all for replying!
Mission