• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression for Monthyyyy string match  RSS feed

 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If the input to below program is Jan2011 or January2011 or abc2011, it validates. I want that only full months should succeed. For e.g., Jan2011 is invalid and January2011 is valid.

- Constraint here is I can use only Scanner and findInline methods.


Could you please help in creating a correct regex?

Thanks.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of the [...] brackets. They have a special meaning in a regular expression, and they aren't doing what you need here. Replace them with (...).

you should also remove the +, as you only want the month to appear once (e.g. you don't want "JanuaryMarch2011" to validate).

Finally, although it's not necessary, your outer (...) aren't doing anything useful, so you might as well remove them.
 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Get rid of the [...] brackets. They have a special meaning in a regular expression, and they aren't doing what you need here. Replace them with (...).

You should also remove the +, as you only want the month to appear once (e.g. you don't want "JanuaryMarch2011" to validate).

Finally, although it's not necessary, your outer (...) aren't doing anything useful, so you might as well remove them.


Thanks for the useful reply...
Here, one more issue comes when I use the below mentioned regex:
s.findInLine("^(January|February|March|April|May|June|July|August|September|October|November|December)\\d{4}$");

for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));

Output of above snippet is January, since small brackets are ended before the year \\d{4} regex..but I need the whole string to come as a group.. Other possible solution is to use result.group(). this returns January2011 but I can't use it as my code is generic to other regex patterns also and I am using result.group(i) and not result.group().

Please let me know, if you require more inputs. Would appreciate if you can help in resolving this as well.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does group(0) return?
 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:What does group(0) return?


Yes it returns January2011, the one what I want. It is same as result.group(). But, I am using the loop starting from 1 till the max size. The code is generic and is used for other pattern matching also. So, for this particular pattern, can't change starting from 0.
We need the one starting from 1 and the only option left is to change in regex and not in code.

Please advise.
Thanks.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make the choice a non-capturing group by starting it with (?: instead of just (. Then put the entire thing in a capturing group:
Now you will only have one group, which is the entire String.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Kr. Arora wrote:We need the one starting from 1 and the only option left is to change in regex and not in code.

Ah, that's what the extra brackets were doing, then. You can put them back - though Rob's is a better solution in terms of optimising it.
 
Gaurav Kr. Arora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You can make the choice a non-capturing group by starting it with (?: instead of just (. Then put the entire thing in a capturing group:
Now you will only have one group, which is the entire String.


Got it Rob!! This is what I like the most about Code Ranch! People here dedicate their time to help others!!
Thanks a ton Rob and all of you who contributed to this post!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!