I've got an application which determines, given a perl regex, if it should display a dropdown menu or a simple input field. Therefore, I have to check the regex pattern for an "outer form" and substrings. For this, I came up with several solutions.
Given the input pattern "^(100|500|1000)$", which should result in a drop down menu with three entries, 100, 500 and 1000. I need one regex which parses the entire pattern, to determine if it is a valid list, and one regex that does the actual substring match, since I don't know how to match one substring multiple times. This is my regex pattern:
^\^\((?:((?:[^\|]|\\\|)+)(?:\||(?:\)\$$)))+
A little bit of simplification, since this regex is a little bit fuzzy:
^\^\((?:([\w\d]+)(?:\||(?:\)\$$)))+
This works, but only stores the last substring (1000 in the given case) and throws the rest away, tested with either PCRE and online regex tools. To get the actual substrings, i.e. dropdown menu fields, I have:
(?:\^\()?((?:[^\|]|\\|)+)(?:\||(?:\)\$$))
Simplification again:
(?:\^\()?([\w\d]+)(?:\||(?:\)\$$))
This matches the substring but doesn't match the dropdown menu pattern syntax which the other regex does (this one also matches "^(100|" with substring "100", for example). My question is: is there a way to combine these regular expressions to have just one pattern that matches 1) the entire pattern syntax and 2) the actual substrings?
Thanks in advance,
Jeremy
P.S.: sorry if this is obvious, but I'm very bit tangled about all these regular expressions today.
Input regex: ^(100|500|1000)$
Syntax OK!
Matched substrings: 100, 500, 1000
=> show dropdown menu
Input regex: ^[0-9a-fA-F]+$
Syntax is 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 is wrong!
=> show regular input field
However, to recognize multiple words in any order using regex, I'd suggest the use of quantifier in regex: (\b(james|jack)\b. *){2,} . Unlike lookaround or mode modifier, this works in most regex flavours.
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.
The curly brackets are used to match exactly n instances of the proceeding character or pattern. For example, "/x{2}/" matches "xx".
You can achieve what you need by using two steps.
You could use this regex to validate the format:
\^\(\w+(?:\|\w+)*\)\$
Working demo
Once you validated the right strings you can use a function like 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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With