Click here to get back home

[regex] grep for chars in any order

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
[regex] grep for chars in any order viki 06-18-2008
Posted by Bart Lateur on June 18, 2008, 4:47 pm
Please log in for more thread options
Glenn Jackman wrote:

>At 2008-06-18 09:01AM, "Ben Bullock" wrote:
>> Great! That is better than the solution I posted. But I have an
>> improvement:
>>
>> /(?=.*a)(?=.*b)(?=.*c)/
>
>Nice. I wonder (without bothering to benchmark it) if anchoring the
>expression would be an optimization:
>
> /^(?=.*a)(?=.*b)(?=.*c)/

It would, in case it doesn't match. The latter will only try a match at
the start of the string, the former will try again at every character
position, which is is dead stupid, of course.

Be aware of the possibility of the string containing newlines.

/^(?=.*a)(?=.*b)(?=.*c)/s


--
        Bart.

Posted by nolo contendere on June 18, 2008, 9:40 am
Please log in for more thread options
> How can I build regex that matches all characters of the string $STR
> in any order with =A0.* added between any two characters: ?
> And without generating all N! transpositions (where N is length of
> $STR) ?
> Example.
> For $STR "abc", I want to match equivalent to:
> /(a.*b.*c)|(a.*c.*b)|(b.*a.*c)|(b.*c.*a)|(c.*a.*b)|(c.*b.*a)/
>
> Generating all transpositions is not feasible for larger legths of
> $STR.
> /[abc].*[abc].*[abc]/ is easy and fast but gives false positives.
> What is good solution ?
>

use a math module to get permutations:
http://search.cpan.org/~allenday/Math-Combinatorics-0.09/lib/Math/Combinato=
rics.pm

then from those, build your regexes.

Posted by Jürgen Exner on June 18, 2008, 10:45 am
Please log in for more thread options
>> How can I build regex that matches all characters of the string $STR
>> in any order with  .* added between any two characters:

Maybe I am missing something, but isn't that the same as the text begins
and ends with a character from $str and all the other characters of $str
are included somewhere in the text?
It should be fairly easy to find an algorithm to check for that. You
just need to be careful about how to handle duplicate characters in $STR
and/or the text.

jue

Posted by nolo contendere on June 18, 2008, 10:58 am
Please log in for more thread options
> >> How can I build regex that matches all characters of the string $STR
> >> in any order with =A0.* added between any two characters:
>
> Maybe I am missing something, but isn't that the same as the text begins
> and ends with a character from $str and all the other characters of $str
> are included somewhere in the text?
> It should be fairly easy to find an algorithm to check for that. You
> just need to be careful about how to handle duplicate characters in $STR
> and/or the text.
>

Those are both great points. Perhaps the OP could further refine the
requirements, or state the larger goal.

Posted by Paul Lalli on June 18, 2008, 10:32 am
Please log in for more thread options
> How can I build regex that matches all characters of the string $STR
> in any order with =A0.* added between any two characters: ?
> And without generating all N! transpositions (where N is length of
> $STR) ?
> Example.
> For $STR "abc", I want to match equivalent to:
> /(a.*b.*c)|(a.*c.*b)|(b.*a.*c)|(b.*c.*a)|(c.*a.*b)|(c.*b.*a)/
>
> Generating all transpositions is not feasible for larger legths of
> $STR.
> /[abc].*[abc].*[abc]/ is easy and fast but gives false positives.
> What is good solution ?

#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw/all/;

$STR =3D "whatever";

if (all { $STR =3D~ /$_/ } qw/a b c/) {
print "Matched all of a, b, c\n";
}

__END__

Paul Lalli


Similar ThreadsPosted
cockroach race: grep for characters in any order June 19, 2008, 8:10 am
regex for chars 192 to 255 February 29, 2008, 5:45 am
Regex for special chars.. April 18, 2006, 10:10 am
regex: how to %hash2 = grep %hash1 January 17, 2005, 7:51 am
Matching Multiple Patters In A Regex In Any Order September 26, 2005, 4:35 pm
Delete nonprinting chars September 6, 2004, 10:04 pm
Permuting using any number of given chars May 17, 2005, 10:51 pm
parsing UTF-8 chars out of POST data September 8, 2004, 11:00 am
Matching escaped delimiter chars November 28, 2005, 9:21 pm
Match a number of repeated chars, but NO MORE. December 2, 2005, 2:16 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap