For example I have this text and email:
String text ="For move information please contact us: [email protected], ask Peter to help."
String email="[email protected]";
I want to get two 10 chars sized strings as a result, one from the left side of email, other from the right. Result I want to see: Left-ntact us: Right:, ask Pete
Right now matcher.find() in while loop returns false. And as a result both leftContext and rightContext are null.
Here is my code:
String email = "[email protected]";
int nLeft = 5;
int nRight = 5;
String leftContext;
String rightContext;
String wordDelRegEx = "[^а-яА-Яa-zA-Z0-9.-]+?";
String leftRegEx = "(\\w{" + nLeft + "})";
String rightRegEx = "(\\w{" + nRight + "})";
//matches something like: aaaaaaa [email protected] bbbbbbbbbb
String contextRe = leftRegEx + wordDelRegEx +email + wordDelRegEx + rightRegEx;
String h ="For move information please contact us: [email protected], ask Peter to help."
Pattern pattern = Pattern.compile(contextRe);
Matcher matcher = pattern.matcher(h);
while (matcher.find()) {
leftContext = matcher.group(1);
rightContext = matcher.group(2);
}
System.out.println("Left: " + leftContext);
System.out.println("Right: " + rightContext);