Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean regexMatch(char[] src, char[] regex){
- return regexMatch(src, regex, 0, 0);
- }
- private boolean regexMatch(char[] src, char[] regex, int currIndex, int regexIndex){
- if(regexIndex == regex.length){
- return (currIndex == src.length);
- }
- // At no point in time, [star] cannot be our starting char in regex
- if(regex[regexIndex] == '*'){
- throw new RuntimeException("Invalid Regex ..");
- }
- if(currIndex == src.length){
- // if the current char in regex is not followed by a [star], then there is no match
- while(regex.length > regexIndex+1 && regex[regexIndex+1] == '*'){
- regexIndex += 2;
- }
- return (regex.length == regexIndex);
- }
- // If the char is a dot
- if(regex[regexIndex] == '.'){
- // See if the subsequent char is a [star]
- if(regexIndex+1 < regex.length && regex[regexIndex+1] == '*'){
- // Three cases are possible
- // a. consider there is a match and move the currIndex
- // b. consider there is a match and move both currIndex and regexIndex
- // c. consider there is no match and move the regexIndex
- return regexMatch(src, regex, currIndex+1, regexIndex) || regexMatch(src, regex, currIndex+1, regexIndex+2) || regexMatch(src, regex, currIndex, regexIndex+2);
- } else {
- return regexMatch(src, regex, currIndex+1, regexIndex+1);
- }
- } else {
- // See if the subsequent char is a [star]
- if(regexIndex+1 < regex.length && regex[regexIndex+1] == '*'){
- if(src[currIndex] == regex[regexIndex]){
- // Three cases are possible
- // a. consider there is a match and move the currIndex
- // b. consider there is a match and move both currIndex and regexIndex
- // c. consider there is no match and move the regexIndex
- return regexMatch(src, regex, currIndex+1, regexIndex) || regexMatch(src, regex, currIndex+1, regexIndex+2);
- }
- // NOTE: this represents the (c) case, both when the char does match and when it does not
- return regexMatch(src, regex, currIndex, regexIndex+2);
- } else {
- return src[currIndex] == regex[regexIndex] && regexMatch(src, regex, currIndex+1, regexIndex+1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment