Search code examples
regexcmdfindstr

How to limit the length of a regex


I want to use this regex with FINDSTR, but it's too long, as FINDSTR's search string has to be less than 128 characters.

\b((((Mr)|(Mrs)|(Ms)|(Fr)|(St)|(Sr)|(Rev)|(Dr))\.)\s?)?(((([A-Z][a-z]+)|([A-Z]\.?))(\s(([A-Z][a-z]+)|([A-Z]\.?)))?)|(([A-Z]\.?){1,2}))?\s((([A-Z][a-z]+)\-([A-Z][a-z]+))|(([A-Z][a-z]+)\'([A-Z][a-z]+))|([A-Z][a-z]+))\b

What should I do?


Solution

  • How about:

    (Mr|Mrs|Ms|Fr|St|Sr|Rev|Dr)((\s[A-Z]\.?|[\s\-][A-Z]\w+))*([\s\-][A-Z]\w+)+
    

    That matches:

    • Mr John Smith-Edwards
    • Dr A R. Smith
    • Mr John P. Smith