Summary of regular-expression constructs: http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
package regExpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class regExTest
{
public static void main(String[] args)
{
int count = 3;
int i, pos, from, to;
String[] texts = new String[count];
String regEx = "%[a-zA-Z]+\\w*%";
String matchedString;
Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher;
texts[0] = "-=+";
texts[1] = "%abc%--- abc --- %ba%a a123-";
texts[2] = "a%a23456789_%";
for (i = 0; i < count; i++)
{
pos = 0;
matcher = pattern.matcher(texts[i]);
System.out.println("Original String: " + texts[i]);
while(matcher.find(pos))
{
matchedString = matcher.group();
pos = matcher.end();
from = matcher.start();
to = pos -1;
System.out.println("Matched String: " + matchedString + " (" + from + " ~ " + to + ")");
}
System.out.println("");
}
}
}
실행 결과값
Original String: -=+Original String: %abc%--- abc --- %ba%a a123-
Matched String: %abc% (0 ~ 4)
Matched String: %ba% (17 ~ 20)Original String: a%a23456789_%
Matched String: %a23456789_% (1 ~ 12)