Random search for ordered word through regular expression

I just started with regex in perl. After going through various online lessons, I wanted to write a regular expression that matches words that are independent of the specified word.

I am trying to determine if string "A" consists of a word or a sequence of words in string "B", and I want to do this case insensitive.

For example, if the string "B" is "John Von Neumann", then "JOhn", "Von NeumaNn", "VoN", "john neuMann" will match but lines like "Joh", "NeumaNn VoN", "Vonn" will not match.

I'm not sure how to do this with regular expressions, any idea?

+5
source share
2 answers

Let it ignore the case for a second.

John Von Neumann

can be matched

John Von Neumann    1 1 1
John Von            1 1 0
John     Neumann    1 0 1
John                1 0 0
     Von Neumann    0 1 1
     Von            0 1 0
         Neumann    0 0 1

So the regex pattern you are looking for is

/^(?:John Von Neumann|John Von|John Newmann|John|...)\z/i

Here you can create a list:

sub true_indexes {
   my ($n) = @_;
   my $i = 0;
   my @indexes;
   while ($n) {
      push @indexes, $i if $n & 1;
      ++$i;
      $n >>= 1;
   }
   return @indexes;
}

my @words = split(' ', 'John Von Neumann');

my @patterns;
unshift @patterns, join ' ', @words[ true_indexes($_) ]
   for 1 .. (2**@words)-1;

And finally, we can generate a template:

my $pat = join '|', map quotemeta, @patterns;
my $re = qr/$pat/i;

You would use it like this:

if ($input =~ /^$re\z/) {
   print "match\n";
} else {
   print "no match\n";
}
+9
source

The ikegami solution will take up exponential space to store the string until it is converted to a regular expression (each word will be displayed 2 n - 1 times, where n is the number of words, so the total number of space is at least 2 n - 1 * Sum (word length)). This is not related to the regex engine, since the problem is before the string is converted to a regex.


( , ) ikegami:

^(?=[^ ])(?:(?: |^)John(?= |\z))?+(?:(?: |^)Von(?= |\z))?+(?:(?: |^)Neumann(?= |\z))?+\z

.

:

^
(?=[^ ])
(?:(?: |^)John(?= |\z))?+
(?:(?: |^)Von(?= |\z))?+
(?:(?: |^)Neumann(?= |\z))?+
\z

(?=[^ ]) 2 : , .

?+, ( ), . ? , . , , , , .

, . , .

, . (?= |\z) , (, "John Von Vone", "John Vone").

, ( , ).


, :

^(?= *+[^ ])(?: *+John(?= |\z))?+(?: *+Von(?= |\z))?+(?: *+Neumann(?= |\z))?+ *+\z

( ):

^
(?= *+[^ ])
(?: *+John(?= |\z))?+
(?: *+Von(?= |\z))?+
(?: *+Neumann(?= |\z))?+
 *+
\z

(?= *+[^ ]) , .

, (, ). 0 *, , . - (?= |\z).

- ( ). .


  • :

    aaaaaaaaaaaaaaaaaaa0 aaaaaaaaaaaaaaaaaaa1 ... aaaaaaaaaaaaaaaaaaa9 aaaaaaaaaaaaaaaaaaaa ... aaaaaaaaaaaaaaaaaaaz aaaaaaaaaaaaaaaaaaaA ... aaaaaaaaaaaaaaaaaaaZ
    

    ( 20 , 0-9, a-z, a-z)

    ( ):

    aaaaaaaaaaaaaaaaaaaz aaaaaaaaaaaaaaaaaaay
    

    (y z)

  • :

    patterns used in Perl pattern matching evolved from those supplied
    

    ( )

    ( ):

    patterns used in Perl pattern matching evolved from those suppliedd
    

    ( d )

  • :

    aaaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa aaaaaa aaaaa aaaa
    

    ( a, .)

    ( ):

    aaaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa aaaaaa aaaaa aaaaa
    

    ( a )

  • :

    performance abc xyz performance 456 !@# performance
    

    ( )

    ( ):

    performance performance performance performance
    
+3

All Articles