is there a more elegant regex solution available to matching multiple
patterns in a short string
I've been tearing my hair out for the last two hours with this and can't
help feeling there's a simple solution that I'm not seeing. I am trying to
process a string - a house number (as you would find in a street address)
and break it up into four component parts.
The string can have four basic different patterns
A. a numeric value consisting of one or more digits e.g. 5
B. one or more digits followed by a single alphabetic character e.g. 5A
C. two numeric values consisting of one or more digits and joined by a
hyphen e.g. 5-6
D. two alphanumeric values (with each consisting of one or more digits
followed by a single alphabetic character) split by a hyphen e.g. 5A-6B
the string should always start with a numeric character (1-9) but
everything else is optional
I need to end up with four values as follows
startnumber - it would be 5 in the example above
startsuffix - it would be A in the example above
endnumber - it would be 6 in the example above
endsuffix - it would be B in the example above
startnumber and endnumber can be one or more digits. startsuffix and
endsuffix must be a single alphabetic character
I have some basic validation on my form that only allows 0-9, A-Z and the
'-' character to be input
I've been hacking around with lots of if statements, is_numerics, strpos
and so on but can't help feeling there's a more obvious answer there maybe
using a regex but I'm really struggling. Any help would be gratefully
received
No comments:
Post a Comment