javaregex

Regular expression for pattern "Form 1987A"


I need to extract out a string from a phrase and the extracted string should match a defined pattern. I am using java regex Pattern, Matcher. Only thing I need are the regular expression's for the following pattern strings:

Update 1

Also the regex should find the match only when the phrase starts with "Form" keyword and not when "Form 1987" appears in the middle of the phrase. Ex.

"I am watching Form 1987 Monuments Men" - should not match
"Form 1987 I am watching Monuments Men" - should match


Solution

  • You can also match all form with one regex:

    Form\s\d{4}-?A?
    

    Use double backslash \\ if you want to put it directly into Java code

    DEMO