R
Roy Smith
I'm using java.util.regex from J2SE 1.4.2. I want to match a
fixed-width field which contains a right-justified, possibly
zero-padded, possibly space-padded number. If there are spaces, they
all come before any of the zeros. For example, any of the following
7-character fields should match:
"0004501"
" 4501"
" 04501"
"0000000"
but the following should not:
" "
"4501 "
"0 4501"
It's easy to match exactly 7 digits or spaces:
([ 0-9]{7})
It's easy to match a bunch of optional spaces followed by a zero-padded
integer:
( *[0-9]*)
What I can't figure out is how make the latter pattern only match if the
total length of the field is exactly 7. The only thing I can think of
is to enumerate all the possibilities:
([0-9]{7})| [0-9]{6})|( {2}[0-9]{5}) ... ( {7})
but that's too gross to contemplate seriously.
fixed-width field which contains a right-justified, possibly
zero-padded, possibly space-padded number. If there are spaces, they
all come before any of the zeros. For example, any of the following
7-character fields should match:
"0004501"
" 4501"
" 04501"
"0000000"
but the following should not:
" "
"4501 "
"0 4501"
It's easy to match exactly 7 digits or spaces:
([ 0-9]{7})
It's easy to match a bunch of optional spaces followed by a zero-padded
integer:
( *[0-9]*)
What I can't figure out is how make the latter pattern only match if the
total length of the field is exactly 7. The only thing I can think of
is to enumerate all the possibilities:
([0-9]{7})| [0-9]{6})|( {2}[0-9]{5}) ... ( {7})
but that's too gross to contemplate seriously.