Regex.source
func Regex.source(p: Regex.Pattern): stringRegex.source returns the normalized source of p, in which \/ is written /.
Details
- The source holds the pattern without the slashes around it and without its flags; read the flags with Regex.flags.
- The source is normalized: outside a
\Q...\Espan,\/loses its backslash and a line break becomes\nor\r; an empty pattern becomes(?:). - Two patterns are
==exactly when their sources and their flags are.
Basic examples
Read the source of a literal:
Regex.source(/ant/) // "ant"
Regex.source(/\d+/) // "\\d+"
Regex.source(/a\/b/) // "a/b"The flags are not part of the source:
Regex.source(/hello/i) // "hello"
Regex.flags(/hello/i) // "i"Scope
A pattern from Regex.compile has a source like a literal's. A / needs no escape in the string:
Result.unwrap(Regex.compile("a/b")).source() // "a/b"No literal can write an empty pattern, so build it with Regex.compile. Its source is (?:), the same as the source of /(?:)/:
Result.unwrap(Regex.compile("")).source() // "(?:)"A line break in the compiled string becomes the escape \n, so the source stays on one line:
Result.unwrap(Regex.compile("a\nb")).source() // "a\\nb"A / inside a character class stays as written:
/[a/b]/.source() // "[a/b]"So does a flag group such as (?i):
/(?i)ant/.source() // "(?i)ant"The text of a \Q...\E span is taken as itself, so a \/ inside it keeps its backslash:
/\Qa\/b\E/.source() // "\\Qa\\/b\\E"Call it through . or in a pipe:
let word = /\w+/
word.source() // "\\w+"
word | Regex.source() // "\\w+"Applications
Wrap a pattern so that it matches only whole words, keeping its flags:
func wholeWord(p: Regex.Pattern): Regex.Pattern {
Result.unwrap(Regex.compile("\\b(?:${p.source()})\\b", p.flags()))
}
let cat = wholeWord(/cat/i)
cat.test("a Cat sat") // true
cat.test("concatenate") // falseCombine several patterns into one alternation:
let keywords = [/if/, /else/, /for/] | Array.map(Regex.source) | String.join("|")
keywords // "if|else|for"
Regex.compile("^(?:${keywords})$") // Ok{ value: /^(?:if|else|for)$/ }Store a pattern as data, with its source and flags in separate fields, and build it again later:
type SavedRule = { name: string, source: string, flags: string }
let rule = /^[a-z]+\/\d+$/i
let saved = SavedRule{ name: "id", source: rule.source(), flags: rule.flags() }
println(Json.stringify(saved))
let restored = Result.unwrap(Regex.compile(saved.source, saved.flags))
println(restored == rule){"name":"id","source":"^[a-z]+/\\d+$","flags":"i"}
trueProperties and relations
Compiling the source with the flags gives back an equal pattern:
let p = /^\d{4}-\d{2}$/m
assert(Regex.compile(p.source(), p.flags()) == Ok{ value: p })Patterns are equal when their sources and flags are, so /a\/b/ and a compiled a/b are one pattern:
let p = /a\/b/
let q = Result.unwrap(Regex.compile("a/b"))
assert(p.source() == q.source())
assert(p == q)A pattern renders as a literal, with slashes and flags, while its source has neither:
let p = /a+/i
"${p}" // "/a+/i"
p.source() // "a+"Possible issues
The source alone drops the flags. A pattern compiled from it no longer ignores case:
let p = /ant/i
let q = Result.unwrap(Regex.compile(p.source()))
p.test("ANT") // true
q.test("ANT") // falseA backslash in the source appears doubled when the string is shown, because the REPL shows a string as a string literal. The source itself holds one backslash:
let src = /\d/.source()
src // "\\d"
src.length() // 2See also
Regex.flags, Regex.compile, Regex.escape, Regex.Pattern, Regex
