Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A regular expression is a specialized language for pattern matching. They're used in many text editors and programming languages for string matching. </p> <p>You can do many more complicated things with regular expressions as well. There's a great O'Reilly book on the subject, and numerous examples on the web.</p> <p>The thing that you can't do with regular expressions is proper parsing, because regular expressions aren't a sufficient language to encode a grammar. They're specialized for pattern matching, and if you try to use them for parsing something like XML, you'll likely have problems down the road. More specifically, you can't parse arbitrarily nested recursive structures using regular expressions. A simple example of a problem that a regular expression can't solve well is a set of nested braces like you would find in C:</p> <pre><code>int main() { void func() { } } </code></pre> <p>You can make regular expressions solve this up to a certain point, but the memory requirements for this grow arbitrarily large as the number of braces grows. If you're interested in more detail, read this other StackOverflow question about why such a construct is difficult to parse with regular expressions:</p> <p><a href="https://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns">Can regular expressions be used to match nested patterns?</a></p> <p>Different languages implement regular expressions in different ways, but the Perl implementation is very popular. The family of regular expressions that are compatible with Perl are called PCRE, or <strong>P</strong>erl-<strong>C</strong>ompatible <strong>R</strong>egular <strong>E</strong>xpressions. Here's an example in Perl of a regular expression that can match integers:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; match_string( "one-two" ); match_string( "1-2" ); sub match_string { my $string = shift; if ( $string =~ /(\d+)/ ) { print "$string matches!\n"; print "matched: ", $1, "\n"; } else { print "$string doesn't match!\n"; } } $ perl test.pl one-two doesn't match! 1-2 matches! matched: 1 </code></pre> <p>In this example, the regular expression matches one or more examples of a digit. Here's the line:</p> <pre><code> if ( $string =~ /(\d+)/ ) { </code></pre> <p>The way to read this is:</p> <ul> <li>inside the conditional, the string is being matched against the regular expression between /'s.</li> <li>the \d character translates to a digit, 0-9.</li> <li>the + means "one or more times."</li> <li>the parens () mean capture this match, and put it into a special variable. Because this is the first match, it's put into $1.</li> </ul> <p>In some languages (such as Perl), you can also use regular expressions for doing substitutions, like this:</p> <pre><code>substitute_string( "one-two" ); substitute_string( "1-2" ); sub substitute_string { my $string = shift; print "before: ", $string, "\n"; $string =~ s/1/one/g; $string =~ s/2/two/g; print "after: ", $string, "\n"; } $ perl test.pl before: one-two after: one-two before: 1-2 after: one-two </code></pre> <p>Hopefully that's enough to get you started!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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