正则表达式问题的小问题

正则表达式问题的小问题

正则表达式问题的小问题
怎样用正则表达式找出一个文件中的white space?
在增添一个问题
问题2:

怎样用正则找到一个文件中“最长的词”the longest words?
A quick solution
作者提供的相关的附件(大小:1 K)

I don't quite understand what you meant by "find out", but anyway the following code snippet can print out the whitespace on each line:

[quote]
use strict;
use warnings;

while (<>) {
my $first = 1;
while (/ +/g) {
if ($first) {
print "line $.:";
$first = 0;
}
print " [$&]";
}
print "\n" if !$first;
}
[/quote]

It's fun to run this script against itself:

[quote]
$ perl ws.pl ws.pl
line 1: [ ]
line 2: [ ]
line 4: [ ] [ ]
line 5: [ ] [ ] [ ] [ ]
line 6: [ ] [ ] [ ] [ ]
line 7: [ ] [ ] [ ]
line 8: [ ] [ ] [ ]
line 9: [ ] [ ] [ ]
line 10: [ ]
line 11: [ ] [ ] [ ]
line 12: [ ]
line 13: [ ] [ ] [ ] [ ]
[/quote]

The script file ws.pl is also attached to this post. Enjoy! :)
For the longest word problem
作者提供的相关的附件(大小:1 K)

Wow, you seem to be online at this moment. Cool :)

Finding out the longest word is trivial if we're using Perl :)

[quote]
use strict;
use warnings;

my $max_len = 0;
my $word;
while (<>) {
while (/\w+/g) {
my $len = length($&);
next if $len <= $max_len;
$max_len = $len;
$word = $&
}
}
print "$word\n";
[/quote]

A typical run looks like this:

[quote]
$ perl longest.pl longest.pl
warnings
[/quote]

So "warnings" comes up to be the longest "word" in the input file longest.pl. ;)

My solution is by no means the best and cleanest, but it works. Nit picking welcome :)

Have fun :)




   

Limitations...
Caution, I assumed you were not talking about Chinese words...which could be a nontrivial task ;)

I've got a BIG exam this afternoon, so...have to bbiab ;)

Sorry, to me, entering English is much easier than Chinese :)




   

cheers man~~
ur response so quickly. actually,i sitll confusing for ur codes, but,anyway ,thanks for ur help. hope u get good marks in ur exam this afternoon :)


question 1 and 2
1,how to write a regex that will find a line containing only white space.

2,i want a Perl script that finds the longest word in a text file. The text file will be provided as a command line argument, like #./longestword inputfile.txt

i think probably that is easy for u to understand my questions.

:)
Sorry for such a delay
For Problem 1:

/^\s+$/

For Problem 2:

my previous longest.pl does exactly what you want. Try:

perl longest.pl blah.txt

given that you have a blah.txt in your current directory.

Further questions can be sent directly to my mailbox: agentzh at gmail dot com. Tracking forum threads is really painful and no fun :)

EXAMS ARE FINALLY OVER!!! HOORAY!!!

agentz




   

The missing shebang...
Mind you, I've missed the shebang line in my previously attached scripts:

#!/usr/bin/env perl

Without that I doubt "./longest.pl input.txt" will actually work in bash :)

Yes, "perl longest.pl input.txt" should work everywhere.