Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Q: Regexp for fixed-length string with trailing whitespace

Reply
Thread Tools

Q: Regexp for fixed-length string with trailing whitespace

 
 
andreas1234567@hotmail.com
Guest
Posts: n/a
 
      09-09-2005
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?

--
Andreas

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-09-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:1126266525.428554.270650
@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";
}

> --
> Andreas


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

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-09-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
andreas1234567@hotmail.com
Guest
Posts: n/a
 
      09-09-2005
> > 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.


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?

--
Andreas

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex to match all trailing whitespace _and_ newlines. Dotan Cohen Python 2 10-10-2011 10:23 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
QUERY: strip trailing whitespace ... woes :S KevinGPO C++ 1 02-20-2006 01:48 PM
remove trailing whitespace from string Donald Canton C++ 5 02-09-2004 04:39 PM
Reading words with trailing whitespace Brad Marts C++ 3 11-17-2003 11:31 PM



Advertisments