REGEX
I need to write a regular expression conditionally in one regex pattern str.
EX:
I would like to find the string "EFGH" from any charSequences but not in "CDEFGH" or in "EFGHIJK". But it should match that in "DEFGH" or in "EFGHI". Also when matched it should group only "EFGH" from any charSequences that are obleiging our conditions.
I searched a lot for this, but i can't find no way except writing many regex and trying all them in conditional tags.
But I found a term"non_capturig groups". But I can't understand their concept and also I can't achieve my goal using them. Obviously May be because I did'nt understand them properly.
Need Guidence from experts?
[720 byte] By [
Er.Vela] at [2007-11-27 8:25:00]

Look at Pattern's negative look-behind and look-ahead: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
Use lookups as already specified or complete description like:"(([^C]|)D)?EFGH(IJ[^K]|I[^J]|[^I])?"
> Use lookups as already specified or complete> description like:> > "(([^C]|)D)?EFGH(IJ[^K]|I[^J]|[^I])?"This should work:"(?<!CD)EFGH(?!IJK)">
No :)EFGHI returns false.
String text = "I would like to find the string \"EFGH\" from any "+// index 33
"charSequences but not in \"CDEFGH\" or in \"EFGHIJK\". "+// index 73, 88
"But it should match that in \"DEFGH\" or in \"EFGHI\". "+// index 129, 142
"Also when matched it should group only \"EFGH\" from any "+ // index 190
"charSequences that are obleiging our conditions.";
String regex = "(?<!CD)EFGH(?!IJK)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group()+" -> "+matcher.start());
}
Gives me:
EFGH -> 33
EFGH -> 129
EFGH -> 142
EFGH -> 190
System.out.println( Pattern.matches( "(?<!CD)EFGH(?!IJK)", "EFGHI" ) );Gives me false :)>
> System.out.println(Pattern.matches("(?<!CD)EFGH(?!IJK)", "EFGHI" ) );
>
> Gives me false :)
That can well be: the OP was talking about grouping them, not matching a complete String.
Try it like this:
System.out.println( Pattern.matches( ".*?(?<!CD)EFGH(?!IJK).*?", "EFGHI" ) );
>
> Yeah, that's better :); )
"(?<!CD)EFGH(?!IJK)"
This is working almost fine. But its not working if we introduce optional spaces in non capturing group part.
say like this "(?><!C\s*D\s*)EFGH(?!IJK)"
why its not working and how to achieve this?>
> "(?<!CD)EFGH(?!IJK)"
> This is working almost fine. But its not working if
> we introduce optional spaces in non capturing group
> part.
Well, you should've said so in your original post about the white spaces..
> say like this
> "(?<!C\s*D\s*)EFGH(?!IJK)"
> why its not working and how to achieve this?
Not working? Define that, please.
Error MSg:Exception in thread "main" java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length
near index 11
(?<!C\s*D\s*)EFGH(?!IJK)
^
at java.util.regex.Pattern.error(Pattern.java:1650)
at java.util.regex.Pattern.group0(Pattern.java:2415)
at java.util.regex.Pattern.sequence(Pattern.java:1715)
at java.util.regex.Pattern.expr(Pattern.java:1687)
at java.util.regex.Pattern.compile(Pattern.java:1397)
at java.util.regex.Pattern.><init>(Pattern.java:1124)
at java.util.regex.Pattern.compile(Pattern.java:840)
at RegexTestHarness.main(RegexTestHarness.java:27)
> Error MSg:[code]Exception in thread "main"> java.util.regex.PatternSyntaxException: Look-behind> group does not have an obvious maximum length> near index 11> (?<!C\s*D\s*)EFGH(?!IJK)Well, there is the reason.>
Cool...I did'nt mean it for literally but for conceptually. Any COuld you plz answer my next question-How it could be acieved?
> Cool...I did'nt mean it for literally but for
> conceptually. Any COuld you plz answer my next
> question-How it could be acieved?
Well, you cannot use the * meta-character in that way. So, you'll have to bound your white space characters to a certain maximum. Have a look at the Pattern class to see how you can do that:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
Yes I did that aleady. It is working good. ThanYou.Any links where I could find these concepts (negative and positive look aheads and behnds).anyWay a great Thanx.
> Yes I did that aleady. It is working good. ThanYou.
You're welcome.
> Any links where I could find these concepts (negative
> and positive look aheads and behnds).
> anyWay a great Thanx.
http://www.regular-expressions.info/lookaround.html
(be sure to check out the rest of the tutorial!)