• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regex for month's name

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nope not for months but you can make your own.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic