Q: Regexp for fixed-length string with trailing whitespace

A

andreas1234567

Hi,

I want to create a regexp in order to match N word characters followed
by M trailing space(s), where N+M=<a fixed number>, N>1, M>=0.

Not-quite-what-I-want-to-accomplish sample:

use strict;
use warnings;
my @data = (
"39 ", # ok
"0000 ", # ok
"0139 ", # ok
"000139", # ok
" 0139", # nok
);
for (@data) {
# wanted behavior is to match on 6 word characters, or any word
# characters with trailing space(s), 6 characters long in total.
print "'$_': "; (m/^(\w+\s*){6}$/) ? print "ok\n" : print "nok\n";
}
__END__
'39 ': nok
'0000 ': nok
'0139 ': nok
'000139': ok
' 0139': nok

Any regexp masters online?
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
I want to create a regexp in order to match N word characters followed
by M trailing space(s), where N+M=<a fixed number>, N>1, M>=0.
use strict;
use warnings;

my @data = (
"39 ", # ok
"0000 ", # ok
"0139 ", # ok
"000139", # ok
" 0139", # nok
);

for (@data) {
chomp;
print "$_: ";
(m/^\w{1,6}\s{0,5}$/) ? print "ok\n" : print "nok\n";
}

Your sig separator is wrong. It should be dash-dash-space-newline. As it
is, the space character is missing.

Sinan
 
A

Anno Siegel

Hi,

I want to create a regexp in order to match N word characters followed
by M trailing space(s), where N+M=<a fixed number>, N>1, M>=0.

I'm reading the condition as "N >= 1, M >= 0". If you actually mean
"N > 1" the modifications are simple.
Not-quite-what-I-want-to-accomplish sample:

use strict;
use warnings;
my @data = (
"39 ", # ok
"0000 ", # ok
"0139 ", # ok
"000139", # ok
" 0139", # nok
);
for (@data) {
# wanted behavior is to match on 6 word characters, or any word
# characters with trailing space(s), 6 characters long in total.
print "'$_': "; (m/^(\w+\s*){6}$/) ? print "ok\n" : print "nok\n";
}
__END__
'39 ': nok
'0000 ': nok
'0139 ': nok
'000139': ok
' 0139': nok

Any regexp masters online?

Do you need a single regex to give the whole answer? Otherwise

/(\w+)( *)/ and length( $1) + length( $2) == $n;

would do.

If you need a ready-made regex, the only reasonable way I see is to
construct it from a given $n:

my $re = join '|', map '\\w' x $_ . ' ' x ( $n - $_), 1 .. $n;

It's probably possible to come up with something using (??{ ....}),
but it won't be pretty. I'm not going there.

Anno
 
A

andreas1234567

I want to create a regexp in order to match N word characters followed
I'm reading the condition as "N >= 1, M >= 0". If you actually mean
"N > 1" the modifications are simple.

You are correct.
Do you need a single regex to give the whole answer?

Unfortunately, yes.
my $re = join '|', map '\\w' x $_ . ' ' x ( $n - $_), 1 .. $n;

Excellent! Thanks Anno!
It's probably possible to come up with something using (??{ ....}),
but it won't be pretty. I'm not going there.

Anyone? :)
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top