ff0000e2
ff0000e2

Reputation: 437

Regex to validate company names

I have this RegEx to validate a few things, unfortunately it won't validate P.C. only P.C - I tried adding {0,1} to each period but it still will not validate. Any ideas?

(new-line characters for readability)

/(^|\s)Corporation\.{0,1}(^|$)|
 (^|\s)Corp\.{0,1}(^|$)|
 (^|\s)Inc\.{0,1}(^|$)|
 (^|\s)Incorporated\.{0,1}(^|$)|
 (^|\s)Company\.{0,1}(^|$)|
 (^|\s)(^|$)|
 (^|\s)LTD\.{0,1}(^|$)|
 (^|\s)PLLC\.{0,1}(^|$)|
 (^|\s)P\.{0,1}C\.{0,1}(^|$)/ig;

Upvotes: 0

Views: 4726

Answers (1)

user1919238
user1919238

Reputation:

Here's a simplified version of your regex:

/(?:^|\s)(?:Corporation|Corp|Inc|Incorporated|Company|LTD|PLLC|P\.C)\.?$/ig;
  • {0,1} can be replaced by ?
  • Repetition can be eliminated with some grouping.
  • This doesn't make much sense: (^|$). You are requiring either a beginning of a line or an end of a line to occur right after a match. This is functionally the same as requiring the match to be at the end of the line, so I just replaced it with $.
  • When you need to group things, use non-capturing groups (?:...) unless you need to grab that part of the match. They are more efficient.

All that being said, your original pattern should have matched P.C. at the end of a line. The problem may be something with your input data or the way you are using the regex.

Upvotes: 5

Related Questions