RegEx

The functions in this class allow matching strings and substrings, and extracting substrings, based on matches to regular expression patterns.

RegEx patterns are defined here. The patterns are identical to the ones defined in the Java java.util.regex.Pattern class. This provides a powerful, full-featured regular expression language.

Capturing Groups

Pattern capturing groups are numbered in the order they occur in the pattern, starting at 1. Group 0 corresponds to the match for the entire pattern. Capturing group references may be used in pattern replacement strings. The syntax is '$n' (e.g. "aaa$1bbb"). '\' may be used to escape literal '$' and '\' characters.

Resources

The following websites provide regular expressions for many common patterns.

Examples


String RegEx.extract( String input, String pattern, int groupIndex )
Extracts the substring which matches to a specified group in the pattern.

Parameters:
input - The input string
pattern - The pattern to match to
groupIndex - The index of the group to extract
Returns:
The substring which matches the index'th group.

String RegEx.extract( String input, String pattern )
Extracts the substring which matches the first group in the pattern or the entire pattern, if it does not contain a group.

Parameters:
input - The input string
pattern - The pattern to match to
Returns:
The matched substring.

boolean RegEx.find( String input, String pattern )
Tests whether the input string contains a substring matching the pattern.

Parameters:
input - The input string
pattern - The pattern to match to
Returns:
true if a substring matches the pattern

boolean RegEx.matches( String input, String pattern )
Tests whether the input string matches the pattern.

Parameters:
input - The input string
pattern - The pattern to match to
Returns:
true if the string matches the pattern

boolean RegEx.replaceFirst( String input, String pattern, String replacement )
Replaces the first occurence of the patter with the replacement string. The replacement string may contain capturing group references.

Parameters:
input - The input string
pattern - The pattern to match to
replacement - The replacement string
Returns:
a new string with any replacements

boolean RegEx.replaceAll( String input, String pattern, String replacement )
Replaces all occurences of the patter with the replacement string. The replacement string may contain capturing group references.

Parameters:
input - The input string
pattern - The pattern to match to
replacement - The replacement string
Returns:
a new string with any replacements

String RegEx.splitAt( String input, String pattern, int index )
Splits the input around matches of the given pattern, and returns the index'th split substring. Indexing starts at 0.

Parameters:
input - The input string
pattern - The pattern to match to
index - The index of the split substring to return
Returns:
The index'th split substring