RE: Help needed on regexp formats

2003-12-04 Thread Mirabito, Massimo
Joseph,

Thanks for taking the time to explain regexp. This works great you saved
me a lot of time

Take care and happy holidays, 

Max

-Original Message-
From: Walid Joseph Gedeon [mailto:[EMAIL PROTECTED] 
Sent: Thursday, December 04, 2003 7:59 AM
To: Regexp Users List
Cc: [EMAIL PROTECTED]
Subject: Re: Help needed on regexp formats
Importance: High

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 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Help needed on regexp formats

2003-12-04 Thread Walid Joseph Gedeon
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 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]