2015-09-21

‎See als o

← Older revision

Revision as of 19:08, 21 September 2015

(One intermediate revision by the same user not shown)

Line 8:

Line 8:

Regular expressions generally involve two stages in [[W:Parsing|parsing]], the first stage involves variable substitution, in the second stage the pattern and string are sent to the RE engine.

Regular expressions generally involve two stages in [[W:Parsing|parsing]], the first stage involves variable substitution, in the second stage the pattern and string are sent to the RE engine.



Regular expression
engies
are [[W:Lexical analysis|token engines]] where a token is an [[W:Assertion (computing)|assertion]] or an atom. An assertion tests some property of the string, zero width assertions such as ''\b'' are termed assertions whereas assertions with nonzero-width are atoms, such as any [[W:ASCII]] or [[W:binary]] character. Internally the token engine matches from left to right a string to a regular expression pattern consuming the string whether the match is satisfied or not.

+

Regular expression
engines
are [[W:Lexical analysis|token engines]] where a token is an [[W:Assertion (computing)|assertion]] or an atom. An assertion tests some property of the string, zero width assertions such as ''\b'' are termed assertions whereas assertions with nonzero-width are atoms, such as any [[W:ASCII]] or [[W:binary]] character. Internally the token engine matches from left to right a string to a regular expression pattern consuming the string whether the match is satisfied or not.

;When a regexp can match a string in several different ways, we can use the principles above to predict which way the regexp will match:

;When a regexp can match a string in several different ways, we can use the principles above to predict which way the regexp will match:

Line 20:

Line 20:

*Principle 3: If there are two or more elements in a regexp, the leftmost greedy quantifier, if any, will match as much of the string as possible while still allowing the whole regexp to match. The next leftmost greedy quantifier, if any, will try to match as much of the string remaining available to it as possible, while still allowing the whole regexp to match. And so on, until all the regexp elements are satisfied.

*Principle 3: If there are two or more elements in a regexp, the leftmost greedy quantifier, if any, will match as much of the string as possible while still allowing the whole regexp to match. The next leftmost greedy quantifier, if any, will try to match as much of the string remaining available to it as possible, while still allowing the whole regexp to match. And so on, until all the regexp elements are satisfied.



==An example==

+

== An example ==



<perl>

+

<
source lang="
perl
"
>



"Hello World" =~ m/World/;  # matches

+

"Hello World" =~ m/World/;  # matches



</
perl
>

+

</
source
>

What is this perl statement all about? "Hello World" is a simple double quoted string. World is the regular expression and the // enclosing m/World/ tells perl to search a string for a match. The operator =~ associates the string with the regexp match and produces a true value if the regexp matched, or false if the regexp did not match. In our case, World matches the second word in "Hello World" , so the expression is true.

What is this perl statement all about? "Hello World" is a simple double quoted string. World is the regular expression and the // enclosing m/World/ tells perl to search a string for a match. The operator =~ associates the string with the regexp match and produces a true value if the regexp matched, or false if the regexp did not match. In our case, World matches the second word in "Hello World" , so the expression is true.

Wikipedia provides a useful overview of [http://en.wikipedia.org/wiki/Regular_expression regular expressions] and their history.

Wikipedia provides a useful overview of [http://en.wikipedia.org/wiki/Regular_expression regular expressions] and their history.



==Perl one liners==

+

== Tutorials ==



If you type;

+



<perl>

+



perl --help

+



</perl>

+



on the command line, perl will provide details on its usage and command line switches. The switches we are interested in are

+



<pre>

+



-d[:debugger]  run program under debugger

+



-e program      one line of program (several -e's allowed, omit programfile)

+



-n              assume 'while (<>) { ... }' loop around program

+



</pre>

+



===Using -e switch===

+



<perl>

+



# Unix/Linux

+



perl -e 'print "It matches\n" if "Hello World" =~ /World/;'

+



# Windows

+



perl -e "print \"It matches\n\" if "Hello World" =~ /World/;"

+



</perl>

+



* See [http://www.perlmonks.org/?node_id=945 string literals] for details on differences between single and double quotes.

+



+



===Using -de switch===

+



To initiate the debugger for a one line program type;

+



<perl>

+



perl -de 42

+



# Returns an interactive shell within the perl debugger

+



DB<1>

+



</perl>

+



Now type 'h' to obtain for help commands used within the debugger. Commands useful for one liners in the debugger are;

+



<pre>

+



'x' to eval an expression in list context, and print the result.

+



'q' to quit

+



</pre>

+



+



==Tutorials==

+

*[http://perldoc.perl.org/perlrequick.html Perl regular expressions quick start]

*[http://perldoc.perl.org/perlrequick.html Perl regular expressions quick start]

*[http://perldoc.perl.org/perlretut.html Perl regular expressions tutorial]

*[http://perldoc.perl.org/perlretut.html Perl regular expressions tutorial]

*[http://www.regular-expressions.info/engine.html Regular expression info]

*[http://www.regular-expressions.info/engine.html Regular expression info]



==Reference books==

+

== Reference books ==

*[http://www.oreilly.com/catalog/regex/ Mastering regular expressions]

*[http://www.oreilly.com/catalog/regex/ Mastering regular expressions]



==Engines==

+

== Engines ==

*[http://www.pcre.org/ Perl compatible regular expressions (PCRE)]

*[http://www.pcre.org/ Perl compatible regular expressions (PCRE)]

*[http://laurikari.net/tre/ TRE Regular expression engine in C]

*[http://laurikari.net/tre/ TRE Regular expression engine in C]

*[http://www.regexlab.com/en/deelx/download.htm DEELX Regular expression engine in C++]/[http://www.codeproject.com/useritems/deelx.asp DEELX]

*[http://www.regexlab.com/en/deelx/download.htm DEELX Regular expression engine in C++]/[http://www.codeproject.com/useritems/deelx.asp DEELX]



==Perl==

+

== Perl ==

*[[w:Regular expression|Wikipedia: Regular expressions]]

*[[w:Regular expression|Wikipedia: Regular expressions]]

*[[w:Perl_6_rules|Wikipedia Perl 6 Rules]]

*[[w:Perl_6_rules|Wikipedia Perl 6 Rules]]

Line 80:

Line 47:

*[http://www.programmersheaven.com/2/Perl6-FAQ-Regex Perl 6 regex FAQ]

*[http://www.programmersheaven.com/2/Perl6-FAQ-Regex Perl 6 regex FAQ]



==See also==

+

== See also ==

*[http://perldoc.perl.org/perlrun.html How to execute the Perl interpreter]

*[http://perldoc.perl.org/perlrun.html How to execute the Perl interpreter]

*[http://regexlib.com/DisplayPatterns.aspx Useful regular expression pattern library repository]

*[http://regexlib.com/DisplayPatterns.aspx Useful regular expression pattern library repository]

*[http://txt2re.com txt2re.com] ''- a useful regular expression generator tool''

*[http://txt2re.com txt2re.com] ''- a useful regular expression generator tool''

*[http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ Regular expressions cheatsheet]

*[http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ Regular expressions cheatsheet]

+

*[http://montreal.pm.org/tech/neil_kandalgaonkar.shtml A regular expression to check if a number is prime]

[[Category:Tutorials]][[Category:PERL]]

[[Category:Tutorials]][[Category:PERL]]

Show more