-
Notifications
You must be signed in to change notification settings - Fork 0
Regex pattern cheat sheet
Soumik Sarkar edited this page Mar 3, 2024
·
2 revisions
So you already know regex patterns? Here's how they map to this library.
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative ((<|>), optional, many, some)
import Control.Monad (replicateM)
import Regex.Text (REText)
import qualified Regex.Text as R
import qualified Data.CharSet as CS
-- The examples below use these imports.
Here we use the Text
interface from the module Regex.Text
. To parse
String
s, use Regex.List
instead, which is similar.
Pattern | A possible corresponding RE
|
Type of the RE
|
---|---|---|
a |
R.char 'a' |
REText Char |
[aeiou] |
R.oneOf "aeiou" |
REText Char |
[a-z] |
R.oneOf CS.asciiLower |
REText Char |
[A-F] |
R.oneOf (CS.fromRange ('A','F')) |
REText Char |
[xyz0-9] |
R.oneOf ("xyz" <> CS.digit) |
REText Char |
[^aeiou] |
R.oneOf (CS.not "aeiou") |
REText Char |
. |
R.anyChar [1]
|
REText Char |
cat |
R.text "cat" |
REText Text |
(cat)? |
optional (R.text "cat") |
REText (Maybe Text) |
cat|dog |
R.text "cat" <|> R.text "dog" |
REText Text |
(cat)(dog) |
liftA2 (,) (R.text "cat") (R.text "dog") |
REText (Text, Text) |
(cat)* |
many (R.text "cat") |
REText [Text] |
(cat)+ |
some (R.text "cat") |
REText [Text] |
(cat){5} |
replicateM 5 (R.text "cat") |
REText [Text] |
(cat){5,} |
R.atLeast 5 (R.text "cat") |
REText [Text] |
(cat){,5} |
R.atMost 5 (R.text "cat") |
REText [Text] |
(cat){5,20} |
R.betweenCount (5,20) (R.text "cat") |
REText [Text] |
.* |
R.manyText |
REText Text |
.+ |
R.someText |
REText Text |
[a-z]* |
R.manyTextOf CS.asciiLower |
REText Text |
[a-z]+ |
R.someTextOf CS.asciiLower |
REText Text |
(cat)?? |
R.optionalMin (R.text "cat") |
REText (Maybe Text) |
(cat)*? |
R.manyMin (R.text "cat") |
REText [Text] |
(cat)+? |
R.someMin (R.text "cat") |
REText [Text] |
(cat){5,}? |
R.atLeastMin 5 (R.text "cat") |
REText [Text] |
(cat){,5}? |
R.atMostMin 5 (R.text "cat") |
REText [Text] |
(cat){5,20}? |
R.betweenCountMin (5,20) (R.text "cat") |
REText [Text] |
.*? |
R.manyTextMin |
REText Text |
.+? |
R.someTextMin |
REText Text |
[a-z]*? |
R.manyTextOfMin CS.asciiLower |
REText Text |
[a-z]+? |
R.someTextOfMin CS.asciiLower |
REText Text |
1".
" may exclude newlines depending on the regex implementation,
but anyChar
always means any Char
.
This library always matches the full text, so there is no equivalent of the
markers ^
and $
. Some features, such as boundary assertions, lookahead, and
backreferences, are not supported.
While the above examples show how text matching with regex patterns translates
to this library, do not forget that this is a parsing library! Use it for
parsing appropriate values from Text
when that is the right thing to do.