Complicated regular expression help needed in java

91 Views Asked by At

I need to match the following variants.

annotation: 103810471047
annotation: 1038104710
ANnoTAtION: 1038104710
ANnoTAtION: 103810571057
Annotation: asdf1234-as12-as12-as12-asdf1234asdf-abcd1234asdf1234asdf12
ANNotation: asdf1234-as12-as12-as12-asdf1234asdf-abcd1234asdf1234asdf12

I am having trouble coming up with the right expressions. So far I have tried the following using two look aheads.

\b(\Qannotation\E|)\b((?=[^\w\r\n\.!\?$]{0,5}?)(?:[A-Z\d]+\b|(["']).*?\3))|((?=[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}-[\w]{22}\b)[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}-[\w]{22}\b)|((?=[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}-[\w]{22}\b)[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}-[\w]{22}\b)

But it doesn't seem to match correctly. I need it to match anything that matches case insensitive annotation, follow by either a number between 8-12 digits or a hyphen separated word list with 8,4,4,4,12,22 characters.

It should return 2 groups. first group is the word "annotation" second group is either the 8-12 digit, or the hyphen separate word list with 8,4,4,4,12,22 characters.

1

There are 1 best solutions below

9
On BEST ANSWER

You can try the regex : (?i)annotation:.*?\s(?:(?:\d{8,12})|(?:\w{8}-\w{4}-\w{4}-\w{4}-\w{12}-\w{22}))

Demo here