16. Regular expressions and text extraction
function run()
let found = regex_search("([A-Z]+)-(\\d+)", "KEY-204")
if found == null
return "No code found"
end
return found["groups"][2]
endThe result of a successful single search is a dictionary with text, start, end and groups. Group 0 is the complete match; group 1 is the first parenthesized capture. Start is inclusive and end is exclusive. A failed single search returns null.
Pass true as the third argument to get an array of all matches. A failed all-matches search returns an empty array. A malformed pattern reports a diagnostic and also returns a no-match-style result; check your pattern rather than treating every empty result as valid input.
PCA Script has no general substring or string-method API. Use split/replace for straightforward transformations and a captured regex group when you need a portion of text. Avoid writing a complicated regular expression for a simple yes/no answer.
