0

I'm trying to write a regex to detect any c/o patterns in my address line.
I want my regex to detect patterns like Care Of, c/o, c.o., CareOf, etc.

I've done PO Box, email address validations, etc. in the past, but this is new to me.
I'm not very strong in this area and am still trying at my end, but any help would be greatly appreciated.

Thanks much.


Adding further detail to my question:

-> I'm attempting this in Java (through a server side call) and Javascript (through a client side call).
-> I'm doing this for address validation; we want to make sure that the customer doesn't add Care Of addresses (similar to PO Box validation)

I need to parse through Address Line 1 and Address Line 2 (text boxes) values in my page and show an error message if any of them contains a 'Care Of' value.
Possible inputs: 'Care Of John', 'c/o John', 'C/O John', 'C.O. John'
Required output: If there is a pattern match, I need to show an error message: 'Sorry, we do not ship to C/O addresses'.
  • 2
    Would you mind adding expected input and output? – Benjamin Gruenbaum Oct 21 '13 at 22:22
  • Are you trying to use [regex to parse a street address](http://smartystreets.com/how-to/regex-street-address)? This gets messy. Fast. Consider sending SmartyStreets an email (I work at SmartyStreets) for help on this from the professionals. ;) – Matt Oct 21 '13 at 22:43
  • @BenjaminGruenbaum: Sorry I missed that. I've added further details. – theAspirant Oct 22 '13 at 17:55
  • @Matt: I'm gonna take a deeper look at your site, but I don't believe my team has budget for additional custom APIs. :) – theAspirant Oct 22 '13 at 17:55
  • @theAspirant Doesn't matter -- just ask them your question (which isn't well-suited for Stack Overflow as-is, hence the close votes) and they'll answer via email. And anyway, the API is free for low-volume or non-profit use. – Matt Oct 22 '13 at 17:57

2 Answers2

2

This matches all of your given inputs:

(?i)c(are|.|/) ?o(\.|f)?
Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
  • Thank you @Darrick. I was able to come up with: [^c+|C+|\.+a+|A+|\\+|\.|\/+r+|R+|\.|0+|o+|O+e+|E+\s+0+|o+|O+f+|F+|\.|\s], but clearly yours is more elegant and simpler. :) – theAspirant Oct 23 '13 at 15:23
0

You should probably just have a single expression with a series of alternations in it:

"Care Of|C\/O|C\.O\.|CareOf"

And set it to case-insensitive.

What language are you working in?

Justin R.
  • 23,435
  • 23
  • 108
  • 157