I am trying to create a regular expression that can parse both variables ($src and $src1) and get the result of Prop, Nam and Tra. (Ignoring any information after).

<?php
    $src = "{{Data#Prop=11a|Nam=33a|Tra=46b}}";
    $src1 = "{{Data#Prop=11a|Nam=33a|Tra=46b|extra=384v}}";

    preg_match_all('/\{\{Data#Prop\=([a-z 0-9]*)\|Nam\=([a-z 0-9]*)\|Tra=([a-z 0-9]*)????\}\}/Umis', $src, $st);
    ?>

For both $src and $src1 the script should return $st[0] = 11a; $st[1] = "33a"; $st[2] = "46b;


I need to put a expression where the question marks are '????' that optionally looks for '\|(.*)'
If I include '\|(.*)' $src1 works but $src doesn't work, if I remove '\|(.*)' $src works but $src1 doesn't work.

Hi AntiQuark,
the first array item will always contain the whole captured area so for getting the individual parts, you need to start with key 1.

In your case I would try simplifying the regexp by making it more universal. Let's say it's a sequence of key=value separated with |.

For parsing it I would use (not tested)
\{\{([^=]+=([^\|\}]+)[\|\}])+\}

It says:
1. starts with {{
2. ignore until =
3. then capture everything up to | or }
4. repeat 2. and 3.

You would then need to process the $st array and keep only what you want.

For testing regular expressions, try the java applet here:
http://myregexp.com/
I use this software as an Eclipse plugin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.