aspose file tools*
The moose likes Java in General and the fly likes regex for month's name 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 for month Watch "regex for month New topic
Author

regex for month's name

Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
what can be the regex construct for month's name? Initially, I've put
(jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec).*
So, that if some one writes december or dec, both will be taken up. But later it proved to be naive, as it also takes for example decades. So, I wanted a construct like a word starting with dec and if anything comes up next initial three characters should be emb and nothing else. But I'm unable to formulate it. Please somebody help me.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

Hello there so you want to use regex to check months.

january, february => ends in "ary"
september to december => ends in "ber"

Then at least for these months you can have something like
(jan|feb).*ary or (sep|oct|nov|dec).*ber

But honestly why not use those notations? java tutorial regex test harness can give you a tester for your regex. You may want to look through the regex tutorial for the notations like \w \b or whatever.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
K. Tsang wrote:
But honestly why not use those notations? java tutorial regex test harness can give you a tester for your regex. You may want to look through the regex tutorial for the notations like \w \b or whatever.

thanks Tsang, but what notations you are indicating towards, is there any library or POSIX or something like \w \b is there for months?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2531
    
    8

nope not for months but you can make your own.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Regex doesn't solve all problems. For example, month names don't really follow a pattern, so in order to get it to work you would need multiple different patterns to test. This can be confusing at least, and hard to manage as well.

You might think about going at it a different way. For example, perhaps using a SimpleDateFormat will help you parse out the date string you are looking for more efficiently (especially if you are doing more than just Month).

If you are just using months then maybe the SimpleDateFormat won't work (you run into the same problem with your original regex where "decimal" can be interpreted as "December") Providing a ResourceBundle with the intended month notations might prove easy to manage and maintain. Example, I created the following Properties file, which I can then use as a ResourceBundle


Then when I want to use it I can do this:


And run the test like:


(Yeah, I know, I misspelled both Properties and Resource in the class name :-( )

Using ResourceBundles has its benefits, it is easily localized (different abbreviations for different languages or regions), you can map the key to whatever value you want (I just used the full name, but you could use the Month index, or something else that makes sense), and with PropertyResourceBundle, it is easy to add/modify the values (even as the app is running).


Steve
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
well Luke, I believe this properties file won't suit my requirement. I am dealing with text mining. So, I can expect misspellings as well. In properties file it will hard to put months with their misspellings too and more so when you don't know what new misspelling may come up.
 
wood burning stoves
 
subject: regex for month's name