| 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
|
|
|
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
|
|
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
|
|
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.
|
 |
 |
|
|
subject: Matching mutually exclusive string in regex
|
|
|