Tuesday, August 19, 2008

Regex - Tutorial

This link helps us to understand better about the Regex.

X{n} – means exactly n occurrences of X.
X{n,} – means at least n occurrences of X and there is no limit for maximum occurrence.
X{n,m} – means at least n and at most m occurrences of X.
X+ - means one or more occurrences of X.
x*- means zero or more occurrences of X.

Pattern pattern = Pattern.compile("He*llo");
CharSequence inputStr = "Hallo Hello Heello Hzllo2 H_llo Hillo1";
Matcher matcher = pattern.matcher(inputStr);
while(matcher.find())
{
int start = matcher.start();
int end = matcher.end();
System.out.println("Pattern found: " + inputStr.subSequence(start, end).toString());
}

0 Comments:

Post a Comment

<< Home