So currently I am working with a parsed yaml file that uses Regex to validate string values in a loan schema.
Specifically, right now I am working on postal code validation. I saw the post that provided a way to validate regex for postal codes that contain hyphens and spaces as well.
My current pattern, ^\d{5}(?:[-\s]\d{4})?\s+$\g
, matches the following example formats, with either 5dig or 9dig zips accepted:
12345-1234
12345 1234
12345
I found this solution here at Stack Overflow and have since combined it with this approach to trim whitespace off the ends of the string.
While this originally fit the criteria, my company requested I replace/scrape any hyphens or spaces from the postal code value for the following output
12345-1234 -> 123451234
12345 1234 -> 123451234
12345 -> 12345
I can do this directly in Java but, since I have developed a framework in Java that runs hundreds of different string validations, I'd like to avoid having to make a specific code block just for this one string validation.
Is there an efficient way for me to trim characters from the middle of a string using solely vanilla regex?