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 Matching mutually exclusive string in regex 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 "Matching mutually exclusive string in regex" Watch "Matching mutually exclusive string in regex" New topic
Author

Matching mutually exclusive string in regex

Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
Hi,

My input can be only abj or abr. no other combination of any other or these a,b,j,r characters or abjr is also invalid.
Only "abj" or "abr" are valid to me.. this is a part of another string. So ^ or $ cannot be used.

String input = "abj";
Scanner s = new Scanner(input);
String regex = null;
s.findInLine(regex);
MatchResult result = s.match();
System.out.println("Result: "+ result.group());

Could you please help me in creating the regex for this?

I tried different combinations like "ab[rj]{1}" but it didn't help. It matches with abjr also.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
I think you need to look into the differences between findInLine(), find(), and matches().
Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
Mike Simmons wrote:I think you need to look into the differences between findInLine(), find(), and matches().


Scanner doesn't has find and matches method and I have to use Scanner and findInline method (as it is already present in my framework)
Please help in creating regex.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
Wait... because something is present in your framework, you have to use it? Really? How sad.

I suppose you could put something in the regex to require a line break of some sort after the [rj]. I suggest looking through the java.util.regex.Pattern documentation to find ways to represent this - I believe there are several possibilities.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4166
    
    3

abjr is also invalid.
Only "abj" or "abr" are valid

If that's the complete spec then
What about "abrj" ? "abrr" ?


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Matching mutually exclusive string in regex
 
Similar Threads
Regular Expression for Monthyyyy string match
Pattern.match() problem with $ in String
Regex: several of one type of char or one of another
Regex problem
Searching using Scanner class exp. in K&B SCJP6