RegEx
classin packageklyn.regex
public class RegEx extends Object
└ RegEx

Regular expressions are natively supported by the language, following Perl-like syntax. The RegEx class provides methods for matching, searching, replacing, and splitting strings based on regular expression patterns.

Here are some examples of how to use the RegEx class:

regex = RegEx("^\\d+$")

You can also define regex patterns using raw string literals to avoid escaping backslashes.

regex = RegEx(r"^\d+$")

Notice also that Klyn language support a native syntax like Perl or JavaScript for regex literals, which can be used directly without the need for escaping backslashes.

regex = /^\d+$/

Literals, character classes (including ranges and negated classes), and quantifiers operate on Unicode scalar characters, not individual UTF-8 bytes. A dot matches one character except CR, LF, U+2028 and U+2029. Use [^] to include line terminators. Capture groups keep their original numbering.

assert RegEx("^[a-z\u00E9]+$").matches("caf\u00E9")
assert RegEx("^[^a]$").matches("\U0001F600")
assert not RegEx(".").matches("\n")
Properties
Modifier and Type Member Description
public property flags
flags as Int
Regular-expression option bits.
public property pattern
pattern as String
Regular-expression source.
Inherited Properties
propertyInherited Properties from Object: type
Constructors
Modifier and Type Member Description
public native RegEx
RegEx(pattern as String, flags as Int = 0) throws RegExException
RegEx constructor.
Methods
Modifier and Type Member Description
public find
find(input as String) as MatchResult throws RegExException
Finds the first occurrence of the pattern in the input.
public findAll
findAll(input as String) as Array<MatchResult> throws RegExException
Finds all occurrences of the pattern in the input.
public native matcher
matcher(input as String) as RegExMatcher throws RegExException
Creates a matcher for the regular expression.
public native matches
matches(input as String) as Boolean throws RegExException
Checks if the input matches the pattern.
public native replace
replace(input as String, replacement as String) as String throws RegExException
Replaces all occurrences of the pattern with a replacement string.
public native replaceFirst
replaceFirst(input as String, replacement as String) as String throws RegExException
Replaces the first occurrence of the pattern with a replacement string.
public native replaceLast
replaceLast(input as String, replacement as String) as String throws RegExException
Replaces the last occurrence of the pattern with a replacement string.
public native split
split(input as String) as Array<String> throws RegExException
Splits the input into an array of substrings using the pattern as a delimiter.
Inherited Methods
methodInherited Methods from Object: deepCopy, fromJson, hash, toDict, toJson, toMap, toString, toXml