AutolangDocs
std/regex

Regular Expressions

The std/regex module provides high-performance regular expression capabilities. Autolang supports pattern compilation via the direct constructor Regex(...), string extension toRegex(), or factory method Regex.compile(...) for matching, searching, splitting, and text replacement.

Pattern Initialization & Compilation

Create Regex instances through the direct constructor, string extension method, or static factory. Compiled regex patterns are reusable and optimized for execution.

@import("std/regex") // 1. Direct constructor initialization val invoiceRegex = Regex("^INV-[0-9]{4}-[A-Z]{3}$") println("Compiled pattern: " + invoiceRegex.pattern()) // 2. Extension method initialization toRegex() val whitespaceRegex = "\\s+".toRegex() // 3. Factory initialization via Regex.compile() val phoneRegex = Regex.compile("^\\+?[0-9]{10,12}$") println("Regex patterns compiled successfully")

Validation & Pattern Matching

The matches() method requires the entire string to match the pattern, while isMatch() and containsMatchIn() test for partial matches within text.

@import("std/regex") val codePattern = Regex("^[A-Z]{2}-[0-9]{6}$") val validCode = "US-849201" val invalidCode = "US-849201-EXTRA" // Exact full match check println("Valid code matches: " + codePattern.matches(validCode)) // true println("Invalid code matches: " + codePattern.matches(invalidCode)) // false // String extension matches() println("Extension match: " + validCode.matches(codePattern)) // true // Partial match in text val logEntry = "Transaction US-849201 processed successfully" println("Contains match: " + codePattern.containsMatchIn(logEntry)) // true println("String contains pattern: " + logEntry.contains(codePattern))

Searching & Data Extraction

The find() method returns the first matched substring, while findAll() returns an array containing all matching occurrences.

@import("std/regex") val orderText = "Order PO-102 and PO-883 arrived at warehouse, PO-994 in transit" val poRegex = Regex("PO-[0-9]+") // 1. Find first match val firstPo = poRegex.find(orderText) println("First match: " + firstPo) // PO-102 // 2. Extract all matches val allPos = poRegex.findAll(orderText) println("Total matches: " + allPos.size()) // 3 println("Matches: " + allPos[0] + ", " + allPos[1] + ", " + allPos[2])

Splitting & Text Replacement

Use split() to tokenize text around delimiters and replace() to sanitize or mask sensitive information.

@import("std/regex") // 1. Split text by comma or semicolon val delimiterRegex = Regex("[,;]") val tags = delimiterRegex.split("finance,billing;audit,report") println("Tags count: " + tags.size() + ", first: " + tags[0]) // 4, finance // Split via string extension val items = "alpha:beta;gamma".split(Regex("[:;]")) println("Items count: " + items.size()) // 3 // 2. Mask credit card or sensitive identifiers val maskDigits = Regex("[0-9]{4}") val rawRecord = "Account 4111-2222-3333-4444" val masked = rawRecord.replace(maskDigits, "****") println("Masked: " + masked) // 3. Static replacement via Regex.replace val sanitized = Regex.replace("[0-9]+", "Order ID: 994", "#") println("Sanitized: " + sanitized)

Method Reference

Constructors & Static Methods

  • Regex(pattern: String): Regex: Initializes a regular expression object.
  • Regex.compile(pattern: String): Regex: Compiles a pattern string into a Regex object.
  • Regex.isMatch(pattern: String, text: String): Bool: Statically checks whether text matches pattern.
  • Regex.replace(pattern: String, text: String, replacement: String): String: Statically replaces pattern matches in text.

Regex Instance Methods

  • pattern(): String: Returns the underlying pattern string.
  • matches(text: String): Bool: Checks whether the entire string matches the pattern.
  • isMatch(text: String): Bool / containsMatchIn(text: String): Bool: Checks whether the pattern appears anywhere in the text.
  • find(text: String): String: Finds and returns the first matched substring.
  • findAll(text: String): Array<String>: Returns an array of all matched substrings.
  • split(text: String): Array<String>: Splits text around matches of the pattern.
  • replace(text: String, replacement: String): String: Replaces all pattern matches with the specified replacement string.

String Extension Methods

  • String.toRegex(): Regex: Converts a string to a regular expression.
  • String.matches(regex: Regex): Bool: Checks whether the entire string matches the regex.
  • String.contains(regex: Regex): Bool: Checks whether the string contains the regex pattern.
  • String.split(regex: Regex): Array<String>: Splits the string using a regex delimiter.
  • String.replace(regex: Regex, replacement: String): String: Replaces occurrences matching the regex.