// retoor <retoor@molodetz.nl>
import "regex" for Regex, Match
System.print("=== Regex Module Demo ===\n")
System.print("--- Basic Pattern Matching ---")
var pattern = Regex.new("hello")
System.print("Pattern: %(pattern.pattern)")
System.print("Test 'hello world': %(pattern.test("hello world"))")
System.print("Test 'HELLO world': %(pattern.test("HELLO world"))")
System.print("Test 'goodbye': %(pattern.test("goodbye"))")
System.print("\n--- Case Insensitive Matching ---")
var insensitive = Regex.new("hello", "i")
System.print("Pattern: %(insensitive.pattern) (flags: %(insensitive.flags))")
System.print("Test 'HELLO world': %(insensitive.test("HELLO world"))")
System.print("Test 'HeLLo': %(insensitive.test("HeLLo"))")
System.print("\n--- Digit Patterns ---")
var digitPattern = Regex.new("[0-9]+")
System.print("Pattern: [0-9]+")
System.print("Test '123': %(digitPattern.test("123"))")
System.print("Test 'abc': %(digitPattern.test("abc"))")
System.print("Test 'abc123': %(digitPattern.test("abc123"))")
System.print("\n--- Replace First Match ---")
var text = "hello hello hello"
var helloPattern = Regex.new("hello")
var replaced = helloPattern.replace(text, "hi")
System.print("Original: %(text)")
System.print("Replaced first: %(replaced)")
System.print("\n--- Replace All Matches ---")
var replacedAll = helloPattern.replaceAll(text, "hi")
System.print("Replaced all: %(replacedAll)")
System.print("\n--- Split by Pattern ---")
var csv = "apple,banana,cherry,date"
var commaPattern = Regex.new(",")
var parts = commaPattern.split(csv)
System.print("CSV: %(csv)")
System.print("Split parts: %(parts)")
System.print("\n--- Split by Multiple Commas ---")
var csvMultiple = "apple,,banana,,,cherry"
var multiComma = Regex.new(",+")
var cleanParts = multiComma.split(csvMultiple)
System.print("CSV: %(csvMultiple)")
System.print("Split parts: %(cleanParts)")
System.print("\n--- Word Pattern ---")
var wordPattern = Regex.new("[a-zA-Z]+")
System.print("Test 'hello123': %(wordPattern.test("hello123"))")
System.print("Test '12345': %(wordPattern.test("12345"))")
System.print("\n--- Whitespace Handling ---")
var whitespace = Regex.new("\\s+")
var messy = "too many spaces"
var cleaned = whitespace.replaceAll(messy, " ")
System.print("Original: '%(messy)'")
System.print("Cleaned: '%(cleaned)'")
System.print("\n--- Special Character Escapes ---")
var digitEscape = Regex.new("\\d+")
System.print("Pattern: \\d+ (digits)")
System.print("Test 'abc123def': %(digitEscape.test("abc123def"))")
var wordEscape = Regex.new("\\w+")
System.print("Pattern: \\w+ (word chars)")
System.print("Test 'hello_world': %(wordEscape.test("hello_world"))")
var spaceEscape = Regex.new("\\s")
System.print("Pattern: \\s (whitespace)")
System.print("Test 'hello world': %(spaceEscape.test("hello world"))")
System.print("\n--- Anchors ---")
var startAnchor = Regex.new("^hello")
System.print("Pattern: ^hello (start anchor)")
System.print("Test 'hello world': %(startAnchor.test("hello world"))")
System.print("Test 'say hello': %(startAnchor.test("say hello"))")
var endAnchor = Regex.new("world$")
System.print("Pattern: world$ (end anchor)")
System.print("Test 'hello world': %(endAnchor.test("hello world"))")
System.print("Test 'world hello': %(endAnchor.test("world hello"))")
System.print("\n--- Quantifiers ---")
var optional = Regex.new("colou?r")
System.print("Pattern: colou?r (optional u)")
System.print("Test 'color': %(optional.test("color"))")
System.print("Test 'colour': %(optional.test("colour"))")
var oneOrMore = Regex.new("a+")
System.print("Pattern: a+ (one or more a)")
System.print("Test 'aaa': %(oneOrMore.test("aaa"))")
System.print("Test 'bbb': %(oneOrMore.test("bbb"))")
var zeroOrMore = Regex.new("ba*b")
System.print("Pattern: ba*b (zero or more a)")
System.print("Test 'bb': %(zeroOrMore.test("bb"))")
System.print("Test 'bab': %(zeroOrMore.test("bab"))")
System.print("Test 'baaab': %(zeroOrMore.test("baaab"))")
System.print("\n--- Character Classes ---")
var vowels = Regex.new("[aeiou]")
System.print("Pattern: [aeiou] (vowels)")
System.print("Test 'hello': %(vowels.test("hello"))")
System.print("Test 'xyz': %(vowels.test("xyz"))")
var notDigit = Regex.new("[^0-9]+")
System.print("Pattern: [^0-9]+ (non-digits)")
var numbersRemoved = notDigit.replaceAll("abc123def456", "")
System.print("Remove non-digits from 'abc123def456': '%(numbersRemoved)'")
System.print("\n--- Practical: Trim Whitespace ---")
var leadingSpace = Regex.new("^\\s+")
var trailingSpace = Regex.new("\\s+$")
var padded = " hello world "
var trimmed = leadingSpace.replace(padded, "")
trimmed = trailingSpace.replace(trimmed, "")
System.print("Original: '[%(padded)]'")
System.print("Trimmed: '[%(trimmed)]'")
System.print("\n--- Practical: Simple Tokenizer ---")
var tokenSeparator = Regex.new("[\\s,;]+")
var sentence = "hello, world; foo bar"
var tokens = tokenSeparator.split(sentence)
System.print("Input: %(sentence)")
System.print("Tokens: %(tokens)")
System.print("\n--- Match Object ---")
var emailRe = Regex.new("(\\w+)@(\\w+)\\.(\\w+)")
var match = emailRe.match("Contact: alice@example.com for info")
if (match != null) {
System.print("Full match: %(match.text)")
System.print("Start index: %(match.start)")
System.print("End index: %(match.end)")
System.print("group(0): %(match.group(0))")
System.print("group(1): %(match.group(1))")
System.print("group(2): %(match.group(2))")
System.print("group(3): %(match.group(3))")
}
System.print("\n--- Groups Property ---")
var dateRe = Regex.new("(\\d{4})-(\\d{2})-(\\d{2})")
var dateMatch = dateRe.match("Date: 2024-01-15")
if (dateMatch != null) {
System.print("groups[0] (full match): %(dateMatch.groups[0])")
System.print("groups[1] (year): %(dateMatch.groups[1])")
System.print("groups[2] (month): %(dateMatch.groups[2])")
System.print("groups[3] (day): %(dateMatch.groups[3])")
System.print("Total groups: %(dateMatch.groups.count)")
}
System.print("\n--- Find All Matches ---")
var numRe = Regex.new("\\d+")
var allMatches = numRe.matchAll("Order 123 has 5 items at $99 each")
System.print("Found %(allMatches.count) numbers:")
for (m in allMatches) {
System.print(" '%(m.text)' at position %(m.start)-%(m.end)")
}