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 "Mirabito, Massimo" <mc...@cdc.gov> on 2003/12/04 12:49:11 UTC

Help needed on regexp formats

I am really new to regexp so please excuse me if I am not wording the
question correctly.

I am having difficulty in validating a string. This string might have
any of the following formats (# denotes a digit)
 
#
#:
##:
##:#
##:##

#
#.
##.
##.#
##.##


I tried several permutations and it works with some formats but not
others. I really need it to handle all the formats above how do I build
it

//RE r = new RE("[0-9][0-9]?\\:|\\.[0-9]?[0-9]?");
//RE r = new RE("[0-9][0-9]?\\:|\\.[0-9]?[0-9]?");
RE r = new RE("[0-9]{1,2}\\:|\\.[0-9]?[0-9]?");

// need to return always true
System.out.println(r.match("1"));
System.out.println(r.match("11"));
System.out.println(r.match("11:"));
System.out.println(r.match("11:1"));
System.out.println(r.match("11:11"));
	
System.out.println(r.match("1"));
System.out.println(r.match("11"));
System.out.println(r.match("11."));
System.out.println(r.match("11.1"));
System.out.println(r.match("11.11"));

//need to return always false if the string has other characters
embedded or does not
// match the pattern
System.out.println(r.match("1a"));
System.out.println(r.match("1a1"));
System.out.println(r.match("11a:"));
System.out.println(r.match("11a:1"));
System.out.println(r.match("11:1a1"));
System.out.println(r.match("a2"));
System.out.println(r.match("22a"));
System.out.println(r.match("22:a"));
System.out.println(r.match("a22a:2"));
System.out.println(r.match("22:2a"));
System.out.println(r.match("333:3"));
System.out.println(r.match("44:444"));


Thanks in advance for any help

Max


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


Re: Help needed on regexp formats

Posted by Walid Joseph Gedeon <wg...@nerim.net>.
Hello Max,

    Following your examples, you are trying to say that there could be
either one or two digits at the beginning followed by an optional rest
which would start with either a colon or a dot, and would itself
optionally contain either one or two digits.

Expr           ::= OneOrTwoDigits [ ColonOrDot [OneOrTwoDigits]]
OneOrTwoDigits ::= DIGIT [ DIGIT ]
ColonOrDot     ::= ":" | "."

    So: either one or two digits you got that correctly: [0-9]{1,2}
The colon and dot in your regexp are not declared as optional so they will
always be expected, instead try this:
        "^[0-9]{1,2}((\\:|\\.)([0-9]{1,2})?)?$"

    The final $ is there to reject the last use case in your example below.

Hope that helps :)

PS: You may also use [:digit:] instead of [0-9] for readability :)

Here is your test prog re-written:

        RE r = new RE("^[:digit:]{1,2}((\\:|\\.)([:digit:]{1,2})?)?$");

        String[] trues = {
            "1", "11", "11:", "11:1", "11:11",
            "11.", "11.1", "11.11",
            "1a", "1a1", "11a:", "11a:1", "11:1a1",
            "a2", "22a", "22:a", "a22a:2", "22:2a", "333:3", "44:444"
        };
        for (int i=0; i<trues.length; i++) {
            System.out.println("["+trues[i]+"] \t "
                +(r.match(trues[i])?"OK":"KO"));
        }

> I am really new to regexp so please excuse me if I am not wording the
> question correctly.
>
> I am having difficulty in validating a string. This string might have
> any of the following formats (# denotes a digit)
>
> #
> #:
> ##:
> ##:#
> ##:##
>
> #
> #.
> ##.
> ##.#
> ##.##
>
>
> I tried several permutations and it works with some formats but not
> others. I really need it to handle all the formats above how do I build
> it
>
> //RE r = new RE("[0-9][0-9]?\\:|\\.[0-9]?[0-9]?");
> //RE r = new RE("[0-9][0-9]?\\:|\\.[0-9]?[0-9]?");
> RE r = new RE("[0-9]{1,2}\\:|\\.[0-9]?[0-9]?");
>
> // need to return always true
> System.out.println(r.match("1"));
> System.out.println(r.match("11"));
> System.out.println(r.match("11:"));
> System.out.println(r.match("11:1"));
> System.out.println(r.match("11:11"));
>
> System.out.println(r.match("1"));
> System.out.println(r.match("11"));
> System.out.println(r.match("11."));
> System.out.println(r.match("11.1"));
> System.out.println(r.match("11.11"));
>
> //need to return always false if the string has other characters
> embedded or does not
> // match the pattern
> System.out.println(r.match("1a"));
> System.out.println(r.match("1a1"));
> System.out.println(r.match("11a:"));
> System.out.println(r.match("11a:1"));
> System.out.println(r.match("11:1a1"));
> System.out.println(r.match("a2"));
> System.out.println(r.match("22a"));
> System.out.println(r.match("22:a"));
> System.out.println(r.match("a22a:2"));
> System.out.println(r.match("22:2a"));
> System.out.println(r.match("333:3"));
> System.out.println(r.match("44:444"));
>
>
> Thanks in advance for any help
>
> Max


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