How to install Regex
There are two repositories included in this project.
- regex. This repository consists of all the files for the actual regular expression engine.
- regexTester. The src directory in this repo contains karthik.regexTester.RegexTester that can be used to run automated tests on the regular expression engine. RegexTester.java is the test program and regex_test_cases.txt contains a list of test regular expressions and strings. Just compile and run RegexTester.java to see a list of command line options.
To use the regular expression engine, just download and compile. The regex package itself does not have a main class to run so it needs to be called from another program like the regexTester.java program.
How to use Regex
If you are familiar with the java.util.regex API, this engine is set up to have a very similar interface. There is no MatchResult object though - the results are obtained by calling methods of the Matcher instance returned by Pattern.
The objects visible to a user are called Matcher and Pattern. First call Pattern.compile(regex_string).
This returns a Matcher object(let’s call it my_matcher
).
You can now use this Matcher object to find matches in a given string by calling my_matcher.find(input_string)
.
my_matcher.matchCount()
- Returns the number of matches found in the last call of my_matcher.matches()
my_matcher.groupCount(int match_num)
- Returns the number of subgroups found in the last call of my_matcher.matches()
in match number match_num
my_matcher.group(int match_num, int group_num)
- Returns the string found in the last call of my_matcher.matches()
in group group_num
of match match_num
The last two calls above will throw exceptions if match_num
or group_num
are greater than the number of matches or groups present.