UPdate.
This commit is contained in:
Vendored
+33
-1
@@ -1,6 +1,6 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "regex" for Regex
|
||||
import "regex" for Regex, Match
|
||||
|
||||
System.print("=== Regex Module Demo ===\n")
|
||||
|
||||
@@ -128,3 +128,35 @@ 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)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user