Note that there are some explanatory texts on larger screens.

plurals
  1. POMake a Perl-style regex interpreter behave like a basic or extended regex interpreter
    text
    copied!<p>I am writing a tool to help students learn regular expressions. I will probably be writing it in Java.</p> <p>The idea is this: the student types in a regular expression and the tool shows which parts of a text will get matched by the regex. Simple enough.</p> <p>But I want to support several different regex "flavors" such as:</p> <ul> <li>Basic regular expressions (think: grep)</li> <li>Extended regular expressions (think: egrep)</li> <li>A subset of Perl regular expressions, including the character classes \w, \s, etc.</li> <li>Sed-style regular expressions</li> </ul> <p>Java has the java.util.Regex class, but it supports only Perl-style regular expressions, which is a superset of the basic and extended REs. What I think I need is a way to take any given regular expression and escape the meta-characters that aren't part of a given flavor. Then I could give it to the Regex object and it would behave as if it was written for the selected RE interpreter.</p> <p>For example, given the following regex:</p> <pre><code>^\w+[0-9]{5}-(\d{4})?$ </code></pre> <p>As a basic regular expression, it would be interpreted as:</p> <pre><code>^\\w\+[0-9]\{5\}-\(\\d\{4\}\)\?$ </code></pre> <p>As an extended regular expression, it would be:</p> <pre><code>^\\w+[0-9]{5}-(\\d{4})?$ </code></pre> <p>And as a Perl-style regex, it would be the same as the original expression.</p> <p>Is there a "regular expression for regular expressions" than I could run through a regex search-and-replace to quote the non-meta characters? What else could I do? Are there alternative Java classes I could use?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload