Reputation: 1885
I have a Regular Expression:
'/ abc \( (complexRegex)(,complexRegex)*? \) /Uux'
To match something like: abc(complexStructure,complexStructure,complexStructure)
How to not write complexRegex twice?
Upvotes: 0
Views: 180
Reputation: 198324
In basic PHP, no way (except by using the variable trick as eisberg suggests, but it's still repeated in the regexp itself). In PHP5, I heard they put in Oniguruma (Ruby 1.9's extended regexp engine), but I haven't tried for myself, and can't find a decent reference except what Oniguruma's Wikipedia page says. If it's true, you can have this:
/abc:(?<complexRegex>d.f)(,\g<complexRegex>)*:ghi/
It will match "abc:def,daf,dif:ghi"
, for example, without repeating the pattern (d.f
), by assigning a name to the pattern (complexRegex
).
Upvotes: 2
Reputation: 3771
You are in PHP you can avoid writing your complexRegex by variables!
$complexRegex = 'your complexRegex here';
$regex = '/ abc \( (' . $complexRegex . ')(,' . $complexRegex . ')*? \) /Uux';
Also read about back references in PCRE: http://www.php.net/manual/en/regexp.reference.back-references.php
Upvotes: 1
Reputation: 2852
You can also use backreferences eg
(x[ms]l).com/\1
# the \1 is the backreference which matches the first capture group
will match
xml.com/xml
and
xsl.com/xsl
Upvotes: -1
Reputation: 2313
You can match 0 or 1 comma with ?
'/ abc \((,?(complexRegex))* \)/
I added white space to make it clearer but you should match any number of white spaces wherever required.
Upvotes: 0