[Logo]
Forums Register Login
Using Patterns for Date String
Hi all

i need some suggestions in woking with pattern/matcher classes for dates.What i am doing is giving an input date as "24 Feb 2002".My output should be displayed as :-
//d+//s//w+//d{4},//d+//s//w+//d+ etc (that is all possible combination which if i give as the argument to Pattern.matcher() and using the matcher class,output is the date "24 Feb 2002" )ie i am doing a reverse process.

i was putting the above string in a character array and my idea was to assign d ,w one by one for digits and words respectively and then display the final output as //d+//s//w+//d{4} or whatever.Any better idea?

Thanks in advance
I don't understand what you're trying to do... Do you actually want to parse some string containing dates in a specific format and find them in this string? Or format dates from one formatting into another? If first is true, then the regex to use would be more like: "\\d+\\s\\w+\\s\\d{4}" (notice using backslashes instead of slashes). But you must be aware, that such regex may also match expressions like e.g. '12323434 _123_ 1342', which is not really a date...
If you're actually trying to reformat a date stored as string, take a look at java.text.DateFormat / java.text.SimpleDateFormat API.

Ok now I think I know what you're trying to do. Do you actually want to put in some expression and receive as output regexes that would match it? There is Pattern#quote method, but as far as I know, it only returns literal pattern (no character classes)... Retrieving pattern that uses character classes from a given input sequence doesn't seem like a trivial task.
[ July 30, 2008: Message edited by: Dariusz Kordonski ]
Hi

i am giving the input as "24 Feb 2002" in a string.what my code should do is to produce the possible regular expression for that string like //d+//s//w+//d{4},//d+//s//w+//d+ etc for this case.Hope its clear now.

This thread has been viewed 1434 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 16, 2018 10:18:51.