[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-regexp-user
Subject:    Broken regular expressions
From:       Mike Dougherty <MikeD () san ! rr ! com>
Date:       2001-06-09 6:11:54
[Download RAW message or body]

It seems like a lot of you have been having trouble getting the
org.apache.regexp package to work. I am relatively new to this list so I
can't tell you more than I have been able to figure out so far. I found
the following quote on the ORO page (http://jakarta.apache.org/oro):

> The Jakarta Project is lucky to also be able to offer another 
> regexp solution for you as well as this package. Options are
> a good thing and we will leave which one you choose to use 
> up to you. :-) They are both perfectly good packages, we 
> just consider the Jakarta ORO package to be more a more 
> complete regular expression package. 

So I threw the problems (at least the two that I started working on
previously) at the ORO package and came up with the following results:

> Checking expression '([a-z0-9-]+)': match found [regexp-1]
> Checking expression '([a-z0-9\-]+)': match found [regexp-1]
> Checking expression '([a-z0-9\.]+)': match found [regexp]
> Checking expression '([a-z0-9.-]+)': match found [regexp-1.2]
> Checking expression '([a-z0-9\.\-]+)': match found [regexp-1.2]
> Checking expression '([a-z0-9.\-]+)': match found [regexp-1.2]
> Checking expression '([a-z0-9\.-]+)': match found [regexp-1.2]
> Checking expression '\{?([a-z0-9-]+)\}?': match found [regexp-1]
> Checking expression '^{(\S+)}$': match found [regexp-1.2]
> Checking expression '^.(\S+).$': match found [regexp-1.2]
> 
> Checking expression '([\w\d\-\/]+?)\s+([\w\d\.\-]+)'
>         OS:      Linux
>         Version: 2.0.32

Since these are the same results as what a similar Perl script returns I
feel pretty confident that the troubles can be solved by switching to
the ORO package. The code for getting the ORO package to work is a
little different so I have attached my sample program (as ugly as it is)
so you can see how I did it.

Any questions or comment about the ORO package and how to use it should
be directed to the <oro-user@jakarta.apache.org>.

Hope that helps,
/mike

-- 
******************************************
 Mike Dougherty -- Java Software Engineer
******************************************
["rex.java" (text/plain)]

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

class rex {
    public static void main(String[] args) {
        
        try {
            String testStr = "{regexp-1.2}";
            String[] regs = new String[] {
                "([a-z0-9-]+)",         // all alphanumeric + '-'
                "([a-z0-9\\-]+)",       // all alphanumeric + '\' + '-'
                "([a-z0-9\\.]+)",       // all alphanumeric + '\' + '.'
                "([a-z0-9.-]+)",        // all alphanumeric + '-' + '.'
                "([a-z0-9\\.\\-]+)",    // all alphanumeric + '\' + '.' + '-'
                "([a-z0-9.\\-]+)",      // all alphanumeric + '.' + '\' + '-'
                "([a-z0-9\\.-]+)",      // all alphanumeric + '\' + '.' + '-'
                "\\{?([a-z0-9-]+)\\}?", // all alphanumeric between '{' & '}'
                "^{(\\S+)}$",           // the first non-whitespace between '{' & '}'
                "^.(\\S+).$",           // the first non-whitespace between first and last char
            };
            String testStr2 = "X11; I; Linux 2.0.32 i586";
            String regex2   = "([\\w\\d\\-\\/]+?)\\s+([\\w\\d\\.\\-]+)";

            int regLen = regs.length;
            Perl5Compiler compiler = new Perl5Compiler();
            Perl5Matcher  matcher  = new Perl5Matcher();

            for(int ri = 0; ri < regLen; ri++ ) {
                Pattern pattern = compiler.compile( regs[ri] );
                System.out.print("Checking expression '" + regs[ri] + "': ");
                if( matcher.contains( testStr, pattern ) ) {
                    MatchResult result = matcher.getMatch();
                    System.out.println("match found [" + result.group( 1 ) + "]"); 
                } else {
                    System.out.println("NO match found");
                }

            }
            System.out.println();
            System.out.print("Checking expression '" + regex2 + "'");
            Pattern pattern2 = compiler.compile( regex2 );
            if( matcher.contains( testStr2, pattern2 ) ) {
                MatchResult result2 = matcher.getMatch();
                System.out.println();
                System.out.println( "\tOS:      " + result2.group( 1 ) );
                System.out.println( "\tVersion: " + result2.group( 2 ) );
            } else {
                System.out.println( "NOT found in [" + testStr2 + "]");
            }
        }catch(Exception ex) {
            System.out.println("Exception: " + ex);
        }
    }
}


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic