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]
# 1
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
prometheuzza at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 2
Use lookups as already specified or complete description like:"(([^C]|)D)?EFGH(IJ[^K]|I[^J]|[^I])?"
Michael.Nazarov@sun.coma at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 3
> Use lookups as already specified or complete> description like:> > "(([^C]|)D)?EFGH(IJ[^K]|I[^J]|[^I])?"This should work:"(?<!CD)EFGH(?!IJK)">
prometheuzza at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 4
No :)EFGHI returns false.
Michael.Nazarov@sun.coma at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 5

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

prometheuzza at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 6
System.out.println( Pattern.matches( "(?<!CD)EFGH(?!IJK)", "EFGHI" ) );Gives me false :)>
Michael.Nazarov@sun.coma at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 7

> 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" ) );

>

prometheuzza at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 8
Yeah, that's better :)
Michael.Nazarov@sun.coma at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 9
> Yeah, that's better :); )
prometheuzza at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 10

"(?<!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?>

Er.Vela at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 11

> "(?<!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.

prometheuzza at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 12

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)

Er.Vela at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 13
> 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.>
prometheuzza at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 14
Cool...I did'nt mean it for literally but for conceptually. Any COuld you plz answer my next question-How it could be acieved?
Er.Vela at 2007-7-12 20:14:17 > top of Java-index,Java Essentials,New To Java...
# 15

> 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

prometheuzza at 2007-7-21 22:39:54 > top of Java-index,Java Essentials,New To Java...
# 16
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.
Er.Vela at 2007-7-21 22:39:54 > top of Java-index,Java Essentials,New To Java...
# 17

> 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!)

prometheuzza at 2007-7-21 22:39:54 > top of Java-index,Java Essentials,New To Java...