Java Regular Expression

Hi All,

Please answer my question , i will explain it with example.

String txt = "nithEOoREOR nitho EOR";

From this string i need to extractnithEOoR

I need to get this from the group() of Matcher.

simply i need a pattern which get chacters before EOR

Thanks,

Kith

[316 byte] By [Kitha] at [2007-10-1 14:36:20]
# 1

It sounds like you want either a reluctant quantifier or a positive lookahead or lookbehind.

The pattern for the reluctant quantifier would be like this, I think: String pattern = "(.+?)EOR"

Reluctant quantifiers are mentioned, but not explained, I think, in the Pattern API.

For the lookarounds, use google, or see http://www.regular-expressions.info/lookaround.html

http://www.regular-expressions.info/lookaround2.html

jverda at 2007-7-10 18:16:32 > top of Java-index,Other Topics,Algorithms...
# 2
Group 1 of "(.*?)EOR.*"
sabre150a at 2007-7-10 18:16:32 > top of Java-index,Other Topics,Algorithms...
# 3
Wow am I slow today! :-)
sabre150a at 2007-7-10 18:16:32 > top of Java-index,Other Topics,Algorithms...
# 4

Thanks sabre150, you got that.

This is going with Grouping it seems.

Could u please explain the pattern

.* means any number of chacters . I am not familar with ?

And in your pattern why u put .* at the end

Is there any way to give scores to ur answer?:)

Thanks again.

Kith

Kitha at 2007-7-10 18:16:32 > top of Java-index,Other Topics,Algorithms...
# 5

You need to read the documentation. A forum is not the place to present all the theory on a topic.

For minimal information see the following code sample.

import java.util.regex.*;

public class Test5 {

// The content of the first pair (.*?) is group 1

// The ? is to make it a reluctant match becaue we want it

// to match as soon as possible..

// The final .* says anything can be after the first EOR

private static final Pattern pattern = Pattern.compile("(.*?)EOR.*");

public static void main(String[] args) {

try {

Matcher matcher = pattern.matcher("nithEOoREOR nitho EOR");

if (matcher.matches()) {

String theBitYouWant = matcher.group(1);

System.out.println(theBitYouWant);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

sabre150a at 2007-7-10 18:16:32 > top of Java-index,Other Topics,Algorithms...