import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:^|(?<=[\\D;a-zA-Z(),.:;?!\"'`>]))(?!000|666|9)(?<![Oo][Rr][Dd][Ee][Rr].)(?<![Oo][Rr][Dd][Ee][Rr]..)(?<![Oo][Rr][Dd][Ee][Rr]...)(?<![Oo][Rr][Dd][Ee][Rr].[Nn][Uu][Mm][Bb][Ee][Rr].)(?<![Oo][Rr][Dd][Ee][Rr].[Nn][Uu][Mm][Bb][Ee][Rr]..)(?<![Oo][Rr][Dd][Ee][Rr].[Nn][Uu][Mm][Bb][Ee][Rr]...)(?<![Xx])\\d{3}[ -.=\\n\\r]{0,10}(?!00)\\d{2}[ -.=\\n\\r]{0,10}(?!0000)\\d{4}(?:$|(?=[\\Da-zA-Z(),.:;?!\"'`<= ]))";
final String string = "Order numbers should not get detected if 'X or x' precedes the number. so this is working fine.\n"
+ "x123456789\n"
+ "X123456789\n"
+ "x123-456-789\n"
+ "X123-456-789\n"
+ "123-456-789\n\n\n"
+ "Need to modify the regex pattern to get the match for the list of ordernumbers written like below...along with the word (order number) should be case insensitive. \n\n"
+ "ordernumber123-456-789\n"
+ "order number123-456789\n"
+ "order number 123456789\n"
+ "123-456789\n"
+ "123456789\n"
+ "ordernumber-123456787\n"
+ "ordernumber - 123456789\n"
+ "ordernumber #123456789\n"
+ "ordernumber *anysplcharacter*123456789\n";
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | 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