import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<![#\\d\\{])\\d+(?!\\w*(?:\\}|(?:st|th|rd|nd)\\b))";
final String string = "123abd //should match 123\n"
+ "abc345 //should match 234\n"
+ "ab2123cd // should match 2123\n\n"
+ "{2}: Should NOT Match. //My regex Works\n\n"
+ "{34: Should NOT Match. //My regex matches 4 in {34\n\n"
+ "45}: Should NOT Match. //My regex matches 4 in {45\n\n"
+ "{123}: Should NOT Match. //My regex matches 2 in {123}\n\n"
+ "1st should NOT match\n"
+ "3th should NOT match, since it's wrong anyway\n"
+ "1nd should NOT match, since it's wrong anyway\n\n"
+ "14th //should NOT Match\n"
+ "14 th should Match\n"
+ "like 1st, 2nd, etc or #1, #2, etc";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html