a question of using regular expression

roger.millington at barclaycard.co.uk roger.millington at barclaycard.co.uk
Thu Nov 29 09:05:33 EST 2001


Stephen,

If I understand you right then what follows should do what you want but I
would not use jakarta-regex-1.2 because it has serious flaws. IBM Regex4j is
my favourite, Jakarta ORO is very good and gnu-regexp is very easy to use but
significantly slower than Regex4j, ORO and jakarta-regex.

Roger

import org.apache.regexp.*;

public class Test291101
{    
    public Test291101()
    {
        try
        {
            m_re = new RE("^good[ ]+(.*)");
        }
        catch (RESyntaxException e)
        {
            // Cannot happen for the above RE but the compiler needs
something!
            System.err.println("Problem creating RE, exception = " + e);
        }
    }
    
    public String extract(String subject)
    {
        // Extract the term contained in '(.*)'
        // or null if we didn't get a match
        return m_re.match(subject)?m_re.getParen(1):null;
    }
      
    public static void main(String args[])
    {
        Test291101 tester = new Test291101();
        
        // This should match and return a String.
        String s1 = tester.extract("good morning!");
        System.out.println("[" + s1 + "]");
        
        // Thus should match and return 'null'
        String s2 = tester.extract("morning! you are very good!");
        System.out.println("[" + s2 + "]");
    }
    
    private RE m_re;
}


 -----  Posted via NewsOne.Net: Free (anonymous) Usenet News via the Web  -----
  http://newsone.net/ -- Free reading and anonymous posting to 60,000+ groups
   NewsOne.Net prohibits users from posting spam.  If this or other posts
made through NewsOne.Net violate posting guidelines, email abuse at newsone.net



More information about the Python-list mailing list