1

I am using PCRE in PHP and I need to find a way to generate say an array of all possible matching values. Any ideas?

For example if I had R[2-9]{1} I would want:

R2
R3
R4
R5
R6
R7
R8
R9
HamZa
  • 14,671
  • 11
  • 54
  • 75
ehiller
  • 1,346
  • 17
  • 32
  • 2
    There's no easy way to do that. Can you tell us more about why you're asking this question? What are you trying to accomplish? – Charles Jun 27 '10 at 04:32
  • I have a number of fields that use PCRE for validation already, a number of them are short enough that I want to be to present all the possible options in a – ehiller Jun 27 '10 at 04:36
  • If they're as simple as the one you gave, it would be faster to write code that creates the menus on a case by case basis than something to parse each regex and create all of the available matches (which might be difficult for non-trivial regexes). – Charles Jun 27 '10 at 04:43
  • Although even a simple regexp (eg \d{1,9}) can give an enormous selection for your dropdowns... more than you'd want to create – Mark Baker Jun 27 '10 at 09:45
  • Right but in the case of all of my regex expressions the total amount possible is always less than 50. Thus why I wanted to get this going. I searched considerably last night for a way to do this and came up short. – ehiller Jun 27 '10 at 16:05
  • -> http://stackoverflow.com/questions/1248519/how-can-i-expand-a-finite-pattern-into-all-its-possible-matches – Kuchen Jun 27 '10 at 20:17

1 Answers1

1

PCRE does not have the ability to generate sample strings based on a regular expression. I do not know of a PHP library that does. Libraries that can do this usually support only limited regex flavors and need artificial restrictions for regexes like R[2-9]* that can match an infinite number of strings.

If you only need to do this for very simple regexes like R[2-9] then it shouldn't be to hard to either:

  • Parse the regex in your own code to generate the sample values, or to use another mechanism.
  • Or to use your own mechanism to specify "R followed by a digit between 2 and 9" from which your code can then generate both the regex and the list of sample values.
  • Or, if the regexes are hard-coded in your source code, just to type out the list of values by hand.
Jan Goyvaerts
  • 21,379
  • 7
  • 60
  • 72