Q:

Regular Expression to find a word that starts with 'JD' fixed word length, in java

I am not expert in regex, need your help in the following problem.  
I need to find a word from an string that contains a letter starting with JD the length of the word is known i.e 20.
 For this case suppose String is "Your shipment 6016499344 was delivered at ABC JD014600001678885621 Piece ID".

A:

You could try the below regex with pattern and matcher classes.
"\\bJD\\w{18}\\b"

\b matches between a word character and a non-word character (vice-versa)
Example:
String s =  "Your shipment 6016499344 was delivered at ABC JD014600001678885621 Piece ID";
Matcher m = Pattern.compile("\\bJD\\w{18}\\b").matcher(s);
while(m.find())
{
    System.out.println(m.group());
}

OR
String s =  "Your shipment 6016499344 was delivered at ABC JD014600001678885621 Piece ID";
Matcher m = Pattern.compile("(?<!\\S)JD[A-Za-z\\d]{18}(?!\\S)").matcher(s);
while(m.find())
{
    System.out.println(m.group());
}

Output:
JD014600001678885621

