String.Regex
A library for working with regex. The syntax matches the RegExp
library from JavaScript.
Create
A regular expression as specified in JavaScript.
Try to create a Regex
. Not all strings are valid though, so you get a
`Maybe' back. This means you can safely accept input from users.
import String.Regex as Regex
lowerCase : Regex.Regex
lowerCase =
Maybe.withDefault Regex.never <|
Regex.fromString "[a-z]+"
Note: There are some shorthand character classes like \w
for
word characters, \s
for whitespace characters, and \d
for digits. Make
sure they are properly escaped! If you specify them directly in your code,
they would look like "\\w\\s\\d"
.
Create a Regex
with some additional options. For example, you can define
fromString
like this:
import String.Regex as Regex
fromString : String -> Maybe Regex.Regex
fromString string =
fromStringWith { caseInsensitive = False, multiline = False } string
A regular expression that never matches any string.
Use
Check to see if a Regex is contained in a string.
import String.Regex as Regex
digit : Regex.Regex
digit =
Maybe.withDefault Regex.never <|
Regex.fromString "[0-9]"
-- Regex.contains digit "abc123" == True
-- Regex.contains digit "abcxyz" == False
Split a string. The following example will split on commas and tolerate whitespace on either side of the comma:
import String.Regex as Regex
comma : Regex.Regex
comma =
Maybe.withDefault Regex.never <|
Regex.fromString " *, *"
-- Regex.split comma "tom,99,90,85" == ["tom","99","90","85"]
-- Regex.split comma "tom, 99, 90, 85" == ["tom","99","90","85"]
-- Regex.split comma "tom , 99, 90, 85" == ["tom","99","90","85"]
If you want some really fancy splits, a library like
gren-lang/parser
will probably be easier to use.
Find matches in a string:
import String.Regex as Regex
location : Regex.Regex
location =
Maybe.withDefault Regex.never <|
Regex.fromString "[oi]n a (\\w+)"
places : Array Regex.Match
places =
Regex.find location "I am on a boat in a lake."
-- map .match places == [ "on a boat", "in a lake" ]
-- map .submatches places == [ [Just "boat"], [Just "lake"] ]
If you need submatches
for some reason, a library like
gren-lang/parser
will probably lead to better code in the long run.
Replace matches. The function from Match
to String
lets
you use the details of a specific match when making replacements.
import String.Regex as Regex
userReplace : String -> (Regex.Match -> String) -> String -> String
userReplace userRegex replacer string =
case Regex.fromString userRegex of
Nothing ->
string
Just regex ->
Regex.replace regex replacer string
devowel : String -> String
devowel string =
userReplace "[aeiou]" (\_ -> "") string
-- devowel "The quick brown fox" == "Th qck brwn fx"
reverseWords : String -> String
reverseWords string =
userReplace "\\w+" (.match >> String.reverse) string
-- reverseWords "deliver mined parts" == "reviled denim strap"
The details about a particular match:
match
— the full string of the match.index
— the index of the match in the original string.number
— if you find many matches, you can think of each one as being labeled with anumber
starting at one. So the first time you find a match, that is matchnumber
one. Second time is matchnumber
two. This is useful when paired withreplace
if replacement is dependent on how many times a pattern has appeared before.submatches
— aRegex
can have subpatterns, sup-parts that are in parentheses. This is a array of all these submatches. This is kind of garbage to use, and using a package likegren-lang/parser
is probably easier.
Fancier Uses
Just like split
but it stops after some number of matches.
A library like gren-lang/parser
will probably lead to better code in
the long run.
Just like find
but it stops after some number of matches.
A library like gren-lang/parser
will probably lead to better code in
the long run.
Just like replace
but it stops after some number of matches.
A library like gren-lang/parser
will probably lead to better code in
the long run.