Difference between revisions of "Cookbook/Pattern matching"
< Cookbook
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
+ | == Introduction == |
||
Regular expressions are useful in some situations where the Data.List |
Regular expressions are useful in some situations where the Data.List |
||
library is unwieldy. Posix style regular expressions are available in |
library is unwieldy. Posix style regular expressions are available in |
||
Line 4: | Line 5: | ||
are [also available], including PCRE and TRE-style regexes. |
are [also available], including PCRE and TRE-style regexes. |
||
− | Bryan O'Sullivan has written [http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutorial/ a nice introduction] to using the new regex libraries. |
+ | Bryan O'Sullivan has written [http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutorial/ a nice introduction] to using the new regex libraries. This page describes the <tt>(=~)</tt> interface. |
+ | |||
+ | == Text.Regex interface == |
||
+ | |||
+ | Using the Text.Regex module, expressions can be applied with <tt>matchRegex</tt> and <tt>mkRegex</tt> with <tt>Maybe [String]</tt> results. Documentation for this module can be found on the [http://hackage.haskell.org/package/regex-compat Hackage regex-compat page]. |
||
+ | |||
+ | <blockquote><pre>$ ghci |
||
+ | Prelude> :m +Text.Regex</pre></blockquote> |
||
+ | |||
+ | For boolean-style (either it succeeded or failed) results: |
||
+ | |||
+ | <blockquote><pre>Prelude Text.Regex> matchRegex (mkRegex "a") "aabc" |
||
+ | Just [] |
||
+ | |||
+ | Prelude Text.Regex> matchRegex (mkRegex "z") "aabc" |
||
+ | Nothing</pre></blockquote> |
||
+ | |||
+ | With capturing results in the <tt>[String]</tt>: |
||
+ | |||
+ | <blockquote><pre>Prelude Text.Regex> matchRegex (mkRegex "(a)") "aabc" |
||
+ | Just ["a"] |
||
+ | |||
+ | Prelude Text.Regex> matchRegex (mkRegex "(a+)") "aabc" |
||
+ | Just ["aa"] |
||
+ | |||
+ | Prelude Text.Regex> matchRegex (mkRegex "(a)(b)") "aabc" |
||
+ | Just ["a","b"] |
||
+ | |||
+ | Prelude Text.Regex> matchRegex (mkRegex "(z)") "aabc" |
||
+ | Nothing</pre></blockquote> |
Latest revision as of 12:26, 22 October 2010
Introduction
Regular expressions are useful in some situations where the Data.List library is unwieldy. Posix style regular expressions are available in the core libraries, and a suite of other regular expression libraries are [also available], including PCRE and TRE-style regexes.
Bryan O'Sullivan has written a nice introduction to using the new regex libraries. This page describes the (=~) interface.
Text.Regex interface
Using the Text.Regex module, expressions can be applied with matchRegex and mkRegex with Maybe [String] results. Documentation for this module can be found on the Hackage regex-compat page.
$ ghci Prelude> :m +Text.Regex
For boolean-style (either it succeeded or failed) results:
Prelude Text.Regex> matchRegex (mkRegex "a") "aabc" Just [] Prelude Text.Regex> matchRegex (mkRegex "z") "aabc" Nothing
With capturing results in the [String]:
Prelude Text.Regex> matchRegex (mkRegex "(a)") "aabc" Just ["a"] Prelude Text.Regex> matchRegex (mkRegex "(a+)") "aabc" Just ["aa"] Prelude Text.Regex> matchRegex (mkRegex "(a)(b)") "aabc" Just ["a","b"] Prelude Text.Regex> matchRegex (mkRegex "(z)") "aabc" Nothing