regex matching fixed-width padded fields

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.
 
G

GaryM

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.

I'm not a regex expert but love a puzzle. AFAIK you cannot mandate
lengths with regex (you said it was fixed widtg, but I think the
following meets all you example critera:

"^[\\s]*[0-9]+$"

(Start with zero or more whitespaces but must end with 1 or more
numbers).
 
A

Alan Moore

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.

You can combine the first two approaches by using lookahead to ensure
the field is correctly formatted before you consume it:

(?= *[0-9]+)[ 0-9]{7}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top