JakeCarter Posted May 16, 2022 Posted May 16, 2022 Afternoon all, I am having issues with some regex creation for one of our forms and wanted some pointers or if anyone has had to do anything similar to this. For recruitment they have 3 different types of codes they need to capture when customers raise a request. I have made them separately but now they need to be put on to one field. 123-ABC-A-1234-A ^([0-9]{3})-([A-Z]{3})-([A-Z]{1})-([0-9]{4})-([A-Z]{1})$ 123-ABC-1234 ^([0-9]{3})-([A-Z]{3})-([0-9]{4})$ 123-ABC-A-1234 ^([0-9]{3})-([A-Z]{3})-([A-Z]{1})-([0-9]{4})$ I've spent a few hours trying to combine these in to one statement. Has anyone got any pointers or examples? Many Thanks in advance
Steve Giller Posted May 16, 2022 Posted May 16, 2022 My first test would be alternation, although a combination of not being RegEx experts and knowing precisely what you're trying to achieve means this is just a best guess. 1
James Ainsworth Posted May 16, 2022 Posted May 16, 2022 Hi @Jake Carter Give this one a try ^(\d{3})-([A-Z]{3}-)([A-Z]{1}-)?(\d{4})(-[A-Z]{1})?$ Broken down... (\d{3})- = 3 digits followed by a dash ([A-Z]{3}-) = 3 upper case characters followed by a - ([A-Z]{1}-)? = 1 optional upper case character and optional - (\d{4}) = 4 digits (-[A-Z]{1})? = 1 optional - and uppercase character I'm also no RegEx expert, so I can't say for sure if this covers absolutely every case. See how you go with this and let us know if it works. 1
JakeCarter Posted May 17, 2022 Author Posted May 17, 2022 9 hours ago, James Ainsworth said: Hi @Jake Carter Give this one a try ^(\d{3})-([A-Z]{3}-)([A-Z]{1}-)?(\d{4})(-[A-Z]{1})?$ Broken down... (\d{3})- = 3 digits followed by a dash ([A-Z]{3}-) = 3 upper case characters followed by a - ([A-Z]{1}-)? = 1 optional upper case character and optional - (\d{4}) = 4 digits (-[A-Z]{1})? = 1 optional - and uppercase character I'm also no RegEx expert, so I can't say for sure if this covers absolutely every case. See how you go with this and let us know if it works. This has worked beautifully! Thank you so much The breakdown has really helped, I have got a few more captures to do and its cleared some things up for me Thank you both again for your help
Steve Giller Posted May 17, 2022 Posted May 17, 2022 1 hour ago, JakeCarter said: The breakdown has really helped If the breakdown was helpful, I can really recommend RegEx 101 as a testing/learning tool. You get a great explanation panel as well as syntax checking and a full library of expressions. 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now