RE: Pattern Regular Expressions: Consecutive ORs not handled corr ectl y

Kayiti Devanandam <[email protected]> Tue, 3 May 2005 03:58:54 -0700
Newsgroups gmane.comp.jakarta.oro.user
Message-ID <A3E375FA108EF94496269A5A96AFCAC102E21884@mailwest-e6>
I think the attachment didn't go with the mail, even though I attached it.
Therefore I am copying the whole class here.
______________

import org.apache.oro.text.regex.*;

public class testRegEx {

  public String[]  _strPatterns;  // W3C XML Schema String pattern dimension
  public static PatternCompiler    _compiler = new Perl5Compiler();
  public Pattern[] _patterns = null;

  private testRegEx() { };

  public void setPatterns(String[] strPatterns) {

      if (strPatterns != null) {

        _patterns = new Pattern[strPatterns.length];
        _strPatterns = strPatterns;

        for (int i = 0; i < strPatterns.length; i++) {
            if (strPatterns[i].length() > 0) {
                try {
                    synchronized (_compiler) {
						System.out.println("Inside
testRegEx.setPatterns(), strPatterns[i] ----->>> "+strPatterns[i]);
        	        _patterns[i] = _compiler.compile(strPatterns[i]);
                    }
                }
                catch (MalformedPatternException mpe) {
                    _patterns[i] = null;
                }
            }
        }
      }

      return;
  }

  public boolean match (String s)
  {
	  System.out.println("Inside testRegEx.match(), String --->>> "+s);
	  if (_patterns != null) {

			  boolean valid = false || (_patterns.length == 0);

              PatternMatcher matcher = new Perl5Matcher();

              for (int i = 0; i < _patterns.length; i++){
              	  if (_patterns[i] != null){
              	  	System.out.println("Inside testRegEx.match(),
getPattern() --->>> "+_patterns[i].getPattern());
              	  }
                  if (_patterns[i] != null && matcher.matches(s,
_patterns[i])) {
                      valid = true;
                      System.out.println("Inside testRegEx.match(), Matched
--->>> "+matcher.matches(s, _patterns[i]));
                      break;
                  }
			  }

              return valid;
	  }
	  else return true;
  }

  public static void main(String[] args){

		testRegEx test = new testRegEx();
		String[]  strPattern = new String[1];
		strPattern[0] = "\\d{3}|\\d{3}(.)\\d{2}";
		test.setPatterns(strPattern);
		System.out.println("Validated or Not:
"+test.match(args[0]));
  }

}

Regds
Dev