1

How to write a regex to decide whether a regex is legal or illegal?

e.g. regex start with * is illegal. So the regex match legal regex may be [^\*]+[\s\S]*.

charmpeach
  • 58
  • 1
  • 8
  • 1
    Please google the terms 'Parser' and 'Grammar'. You shouldn't be using regexs to parse most languages. Trying to parse regexs with themselves is like a Shark emerging from a Shark. – David-SkyMesh Aug 19 '13 at 03:48
  • Exact duplicate of [Is it possible to have regexp that matches all valid regular expressions?](http://stackoverflow.com/q/2906848/139010) and [Is there a regular expression to detect a valid regular expression?](http://stackoverflow.com/q/172303/139010). – Matt Ball Aug 19 '13 at 03:55

2 Answers2

3

Regular expressions are meant to match regular languages and since regular expressions aren't, you cannot achieve this. You should probably be using a Parser for this task.

However, programming languages that supports regular expressions have a built-in parser already and you could probably determine if the regular expression is valid or not by trying to instanciate one with the pattern you want to validate.

For instance, in JavaScript you could do something like:

function isValidRegex(regex) {
    try { 
        new RegExp(regex) 
    }
    catch (e) {
        return false;
    }

    return true;
}

isValidRegex('*a'); //false
plalx
  • 42,889
  • 6
  • 74
  • 90
0

This is not possible without using recursive (ex. PCRE) regex. The set of all possible regular expressions is not a regular language.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • thx. I'm try to do a file search and the regex was given by an input field, so it's no way to determine whether the regex is legal or not before I use it to search? Is there any grammar regex must obey? – charmpeach Aug 19 '13 at 03:57