You are viewing a plain text version of this content. The canonical link for it is here.
Posted to regexp-user@jakarta.apache.org by Victor Okunev <un...@yahoo.ca> on 2003/12/08 23:36:56 UTC

different results matching regex with SDK 1.4.2

Hi,

I am trying to verify e-mail address format using the following regular
expression:

"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$"

I am getting different results when I use SDK 1.2.4 and Apache Regexp. It seems
that the latter is less strict. For example, both statements:

new RE(regex).match(fred&bad@acme.org);
new RE(regex).match("fred@ac$me.org");

return true, which is not what I expect.

I am sure I am doing something wrong, but what?

Thanks,

-- Victor


------------- Code example --------------

import org.apache.regexp.*;
import junit.framework.TestCase;

public class Driver extends TestCase {

    protected String regex =
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$";

    public Driver(String name) {
        super(name);
    }

    private void assertNoMatch(String email) throws Exception {
        assertFalse("email.matches(\"" + email + "\")", email.matches(regex));
        assertFalse("new RE(regex).match(\"" + email + "\")", new
RE(regex).match(email));
    }

    private void assertMatch(String email) throws Exception {
        assertTrue("email.matches(\"" + email + "\")", email.matches(regex));
        assertTrue("new RE(regex).match(\"" + email + "\")", new
RE(regex).match(email));
    }

    public void test() throws Exception {
        assertMatch("fred@acme.org");
        assertNoMatch("fred bad@acme.org");
        assertNoMatch("fred&bad@acme.org");
        assertNoMatch("#fred@acme.org");
        assertNoMatch("fred@ac$me.org");
    }
}

______________________________________________________________________ 
Post your free ad now! http://personals.yahoo.ca

---------------------------------------------------------------------
To unsubscribe, e-mail: regexp-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-user-help@jakarta.apache.org


Re: different results matching regex with SDK 1.4.2

Posted by Oleg Sukhodolsky <so...@ppson.spb.ru>.
>    I'm not sure if it's correct or incorrect as far as the definition,
> but it would appear the "-" after the 9 isn't being treated as a
> character, but a range.

I think this is because of the bug 2121 ('.' or '-' in bracket expression
gives unexpected).

With best regards, Oleg.

---------------------------------------------------------------------
To unsubscribe, e-mail: regexp-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-user-help@jakarta.apache.org


Re: different results matching regex with SDK 1.4.2

Posted by Aaron Hughes <aa...@redrocknetworks.com>.
Victor,

   I'm not sure if it's correct or incorrect as far as the definition,
but it would appear the "-" after the 9 isn't being treated as a
character, but a range.

   The following seems to give your desired result.

"^[_A-Za-z0-9\\-]+(\\.[_A-Za-z0-9\\-]+)*@[A-Za-z0-9\\-]+(\\.[A-Za-z0-9\\-]+)*$"

  Perhaps someone more knowledgeable with regexp than myself could tell
you if it's the definition that's incorrect, or the implementation.

Cheers,
Aaron

On Mon, 2003-12-08 at 14:36, Victor Okunev wrote:
> Hi,
> 
> I am trying to verify e-mail address format using the following regular
> expression:
> 
> "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$"
> 
> I am getting different results when I use SDK 1.2.4 and Apache Regexp. It seems
> that the latter is less strict. For example, both statements:
> 
> new RE(regex).match(fred&bad@acme.org);
> new RE(regex).match("fred@ac$me.org");
> 
> return true, which is not what I expect.
> 
> I am sure I am doing something wrong, but what?
> 
> Thanks,
> 
> -- Victor
> 
> 
> ------------- Code example --------------
> 
> import org.apache.regexp.*;
> import junit.framework.TestCase;
> 
> public class Driver extends TestCase {
> 
>     protected String regex =
> "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$";
> 
>     public Driver(String name) {
>         super(name);
>     }
> 
>     private void assertNoMatch(String email) throws Exception {
>         assertFalse("email.matches(\"" + email + "\")", email.matches(regex));
>         assertFalse("new RE(regex).match(\"" + email + "\")", new
> RE(regex).match(email));
>     }
> 
>     private void assertMatch(String email) throws Exception {
>         assertTrue("email.matches(\"" + email + "\")", email.matches(regex));
>         assertTrue("new RE(regex).match(\"" + email + "\")", new
> RE(regex).match(email));
>     }
> 
>     public void test() throws Exception {
>         assertMatch("fred@acme.org");
>         assertNoMatch("fred bad@acme.org");
>         assertNoMatch("fred&bad@acme.org");
>         assertNoMatch("#fred@acme.org");
>         assertNoMatch("fred@ac$me.org");
>     }
> }
> 
> ______________________________________________________________________ 
> Post your free ad now! http://personals.yahoo.ca
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: regexp-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: regexp-user-help@jakarta.apache.org
-- 
Aaron Hughes <aa...@redrocknetworks.com>
Red Rock Networks, Inc.


---------------------------------------------------------------------
To unsubscribe, e-mail: regexp-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: regexp-user-help@jakarta.apache.org