File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes regex doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regex doubt" Watch "regex doubt" New topic
Author

regex doubt

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i want to check if a text field has something like a >=2000 year:
2004 or
2004/2005, for example. (both are aceptable)
i'm using this code:

I've two Qs:
1) can i improve the regex expression?
2) how can i garantee, with regex, that user is writting, for example, 2004/2005 and not 2004/2003?
Thanks in advance


java amateur
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112



2) how can i garantee, with regex, that user is writting, for example, 2004/2005 and not 2004/2003?


You can't. (Well, you could by simply iterating all possible combinations, but I guess that doesn't qualify as a reasonable solution...)
[ October 08, 2004: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
20\\d{2}

it's a good improvement
Thanks a lot!
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Not sure about your exact requirement, but if it is important that one matched value is "less" than the other (2004 < 2005)...

You can get the value that matched using the java.util.regex classes. I found a simple example in
Sun's regex tutorial.

In their simple example they are just printing them out using find() to try to match and group() to get back what matched:

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Carol, good point!

Ian Darwin
author
Ranch Hand

Joined: Aug 03, 2001
Posts: 64
if (sepoca.matches(regex)) {
return true;
}
return false;

This segment is a bit like
if (true) {
return true;
} else {
return false;
}

And when you see it that way you realize that it lacks expressiveness.

You should probably just say:
return sepoca.matches(regex);

which is a complete statement that does the same thing.

Ian

"Brevity is the soul of wit" (and sometimes, in Java,of readability).


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regex doubt