37 Regex
HardFor all regexes command reference this website
This is a string in which we will look for our pattern.
Initialize the regex. Let’s break it down:
\\S: matches any non-white space character*: matches preceding subexpression 0 or more times.@: matches single character ’@’[a-z]: matches any character that is a letter.+: matches preceding subexpression 1 or more times.
Initialize the matcher with the string and regex. Get the number of matches.
For each match, extract it from the string.
matcher.find() returns the first not-yet-extracted match. It is returned as an Option<MatchData>.
We print the matched expression.
regex.cj
import std.regex.*
main() {
var a: String = """
My name is Postman Pat
my email address is postman.pat@notarealemailaddress.com
My cat's name is Jess, and her email is dont.contact@mycat.com
"""
var regex = Regex("[\\S]*@[\\S]*\\.[a-z]+")
var matcher = Matcher(regex, a)
let number_of_matches = matcher.allCount()
for (_ in 0..number_of_matches) {
var help = matcher.find().getOrThrow().matchStr()
println(help)
}
}
// Output:
// postman.pat@notarealemailaddress.com
// dont.contact@mycat.com