Skip to reading
PCA Script Programming

16. Regular expressions and text extraction

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
function run()
    let found = regex_search("([A-Z]+)-(\\d+)", "KEY-204")
    if found == null
        return "No code found"
    end
    return found["groups"][2]
end

The 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.

Page 23 of 357Permalink
Go to page