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'.
asked Oct 21, 2013 at 22:19
5
  • 2
    Would you mind adding expected input and output? Commented Oct 21, 2013 at 22:22
  • Are you trying to use regex to parse a street address? This gets messy. Fast. Consider sending SmartyStreets an email (I work at SmartyStreets) for help on this from the professionals. ;) Commented Oct 21, 2013 at 22:43
  • @BenjaminGruenbaum: Sorry I missed that. I've added further details. Commented Oct 22, 2013 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. :) Commented Oct 22, 2013 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. Commented Oct 22, 2013 at 17:57

2 Answers 2

2

This matches all of your given inputs:

(?i)c(are|.|/) ?o(\.|f)?
answered Oct 22, 2013 at 20:49
Sign up to request clarification or add additional context in comments.

1 Comment

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. :)
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?

answered Oct 21, 2013 at 22:23

2 Comments

you could consolidate the first and last with Care *Of
Thank you fatcat1111 and Brade Allred. I'll give this a shot. I'm trying to validate this in Java and Javascript.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.