Regular expression – how to search for six letters with letters in alphabetical order

I recently created a Perl script to search for words starting with D and E with the following code:

$infile ='words.txt';
open(IN, $infile);
$count = 0;
while ($word = ) {
chomp($word );
if ($word =~ /^d\w*e$/i) {
print "$word\n";
$count++;
}
}
print "$count\n";

I recently decided to fork the code and create a script to search for a word, the word is six letters, and the letters in the word are Alphabetical order (A to Z). I plan to use the Unix standard dictionary located at usr/share/dict/words instead of words.txt. How to achieve this by modifying this code?

It looks like what you really need is an algorithm to check the letters in a given word Whether to arrange in alphabetical order. There are several ways, but the working principle of this subroutine is to split the word into a list of constituent characters, sort the list and recombine it. If the result matches the original word, the word is already sorted.

use strict;
use warnings;

use feature'fc';

for (qw/ a ab ba cab alt effort toffee /) {
print "$_\n" if in_alpha_order($_);
}

sub in_alpha_order {
my $word = fc(shift);
my $new = join'', sort $word =~ /./g;
return $new eq $word;
}

Yield

a
ab
alt
effort

If you really want to By doing this in the expression, you can create a look like

a(?=[az]) | b(?=[bz]) | c(?=[cz] ) ...

This is a program that works in this way. Its output is the same as the output above.

use strict;
use warnings;

my $regex = join'|', map "${_}(?=[$_-z])",'a'..'z';
$regex = qr/^(?:$regex)*.$/i;

for (qw/ a ab ba cab alt effort toffee /) {
print "$_\ n" if $_ =~ $regex;
}

I recently created a Perl script to search for words starting with D and E with the following code :

$infile ='words.txt';
open(IN, $infile);
$count = 0;
while ($word = ) {
chomp($word);
if ($word =~ /^d\w*e$/i) {
print " $word\n";
$count++;
}
}
print "$count\n";

I recently decided to fork the code and create A script to search for a word, the word is six letters, and the letters in the word are in alphabetical order (A to Z). I plan to use the Unix standard dictionary located at usr/share/dict/words instead of using words .txt. How to achieve this by modifying this code?

It looks like what you really need is an algorithm to check if the letters in a given word are in alphabetical order. There are several ways, but The working principle of this subroutine is to split a word into a list of its constituent characters, sort the list and recombine it. If the result matches the original word, the word has been sorted.

use strict;
use warnings;

use feature'fc';

for (qw/ a ab ba cab alt effort toffee /) {
print "$_\n" if in_alpha_order($_);
}

sub in_alpha_order {
my $word = fc(shift);
my $new = join'', sort $word =~ /./g;
return $new eq $word;
}

Yield

a
ab
alt
effort

If you really want to do this in a regular expression, you can build a message like< /p>

a(?=[az]) | b(?=[bz]) | c(?=[cz]) ...

This Is a program that works in this way. Its output is the same as the above output.

use strict;
use warnings;

my $regex = join'|', map "${_}(?=[$_-z])",'a'..'z';
$regex = qr/^(?:$regex )*.$/i;

for (qw/ a ab ba cab alt effort toffee /) {
print "$_\n" if $_ =~ $regex;
}

Leave a Comment

Your email address will not be published.