I would like to write a regular expression in Java to match a sequence of word characters and spaces followed by the sequence of characters “subclass of” a sequence of word characters and spaces:
Example strings which should be matched:
a subclass of b
a and b subclass of c
a and b subclass of c and d
The following strings should not be matched:
subclass of
a subclass of
subclass of b
a subclass of b subclass of c
I tried the following regex:
[a-zA-Z0-9 ]+subclass of[a-zA-Z0-9 ]+
(?:(?!subclass of).)+subclass of(?:(?!subclass of).)+
but they both fall short of what I need.
You can use this regex:
Here is the Live Demo: