pcre - Regex multiple match substring -
i've got application determines, given perl regex, if should display dropdown menu or simple input field. therefore, have check regex pattern "outer form" , substrings. this, came several solutions.
given input pattern "^(100|500|1000)$", should result in drop down menu 3 entries, 100, 500 , 1000. need 1 regex parses entire pattern, determine if valid list, , 1 regex actual substring match, since don't know how match 1 substring multiple times. regex pattern:
^\^\((?:((?:[^\|]|\\\|)+)(?:\||(?:\)\$$)))+
a little bit of simplification, since regex little bit fuzzy:
^\^\((?:([\w\d]+)(?:\||(?:\)\$$)))+
this works, stores last substring (1000 in given case) , throws rest away, tested either pcre , online regex tools. actual substrings, i.e. dropdown menu fields, have:
(?:\^\()?((?:[^\|]|\\|)+)(?:\||(?:\)\$$))
simplification again:
(?:\^\()?([\w\d]+)(?:\||(?:\)\$$))
this matches substring doesn't match dropdown menu pattern syntax other regex (this 1 matches "^(100|" substring "100", example). question is: there way combine these regular expressions have 1 pattern matches 1) entire pattern syntax , 2) actual substrings?
thanks in advance,
jeremy
p.s.: sorry if obvious, i'm bit tangled these regular expressions today.
sample data:
input regex: ^(100|500|1000)$
syntax ok!
matched substrings: 100, 500, 1000
=> show dropdown menu
input regex: ^[0-9a-fa-f]+$
syntax wrong!
=> show regular input field
input regex: ^(foo|bar)$
syntax ok!
matched substrings: "foo", "bar"
=> show dropdown menu
input regex: ^(foo|bar)[0-9]+$
syntax wrong!
=> show regular input field
you can achieve need using 2 steps.
you use regex validate format:
\^\(\w+(?:\|\w+)*\)\$
once validated right strings can use function this:
$str = "^(100|500|1000|2000|3000)$"; $arr = preg_split ("/\w+/" , $str, -1, preg_split_no_empty); print_r($arr);
output:
array ( [0] => 100 [1] => 500 [2] => 1000 [3] => 2000 [4] => 3000 )
Comments
Post a Comment