Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate strings that match a regular expression string?

Is it possible to display the strings that match a regular expression?

Example:

Take the expression /^AD\d{3}/ and display AD999

What I'm doing is validating a string that is pretty simple either containing all numbers, a few characters maybe, and maybe a '-'. I am validating a postal code on form submit against a database of all countries that use a postal code.

I could perform it in Javascript or PHP, if that makes any difference.

like image 614
Rodney Avatar asked Jun 01 '12 17:06

Rodney


People also ask

Can regex be a string?

Regex examples. A simple example for a regular expression is a (literal) string. For example, the Hello World regex matches the "Hello World" string. . (dot) is another example for a regular expression.

What can be matched using (*) in a regular expression?

A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used.

How do you create a string in regex?

Example : ^\d{3} will match with patterns like "901" in "901-333-". It tells the computer that the match must occur at the end of the string or before \n at the end of the line or string. Example : -\d{3}$ will match with patterns like "-333" in "-901-333". A character class matches any one of a set of characters.


1 Answers

No. That sort of feature is not available.

You can try to implement it yourself, but I don't think that's the solution for you. Simply write the messages normally. Not everything must always be dynamic.

I like your way of thinking though.

like image 84
Madara's Ghost Avatar answered Sep 28 '22 04:09

Madara's Ghost