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 Regular Expression for Monthyyyy string match Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regular Expression for Monthyyyy string match" Watch "Regular Expression for Monthyyyy string match" New topic
Author

Regular Expression for Monthyyyy string match

Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
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

Joined: Apr 06, 2010
Posts: 3794
    
    1

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

Joined: Feb 20, 2011
Posts: 37
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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

What does group(0) return?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
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

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3794
    
    1

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

Joined: Feb 20, 2011
Posts: 37
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!!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Regular Expression for Monthyyyy string match
 
Similar Threads
Matching mutually exclusive string in regex
Possessive Quntifiers
Reg Expressions
groupcount() method in MatchResult Interface in Java 5
Scanner and MatchResult