Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Regex to Array lookup question

by AnomalousMonk (Bishop)
on Apr 05, 2020 at 22:59 UTC ( #11115107=note: print w/replies, xml ) Need Help??

in reply to Regex to Array lookup question

... the format somewhat changes with each response ... no established list of all the possibilities. ... they will through a wrench in the works ...

Kinda hard to do pattern matching when there's no pattern. :) Maybe something like the following. Also, please see How to ask better questions using Test::More and sample data and Short, Self-Contained, Correct Example for useful ways of asking questions — both of the monks and of yourself! (Update: Also also, see haukex's Building Regex Alternations Dynamically article for a discussion of building the  $rx_modifier regex.)

c:\@Work\Perl\monks>perl use strict; use warnings; use Test::More 'no_plan'; use Test::NoWarnings; my @Tests = ( [ ',20/tsra_sct,40?si +ze=medium', 'tsra', 'tsra', ], [ ',30/tsra_hi,30 +?size=medium', 'rain', 'tsra', ], [ ',30/rain_sho +wers?size=medium', 'rain', 'rain', ], [ '', 'bkn', ] +, [ '', ], ); my $rx_pre = qr{ (?<= /) }xms; my $rx_post = qr{ (?= [_?]) }xms; my $rx_generic_modifier = qr{ [[:lower:]]+ }xms; my @modifiers = qw(tsra rain sleet bkn skc few ovc); my ($rx_modifier) = map qr{ $rx_pre (?: $_ | $rx_generic_modifier) $rx_post }xms, join '|', map quotemeta, reverse sort @modifiers ; # print "modifiers regex: $rx_modifier \n"; # for debug VECTOR: for my $ar_vector (@Tests) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, @expected_modifiers) = @$ar_vector; my @got_modifiers = $string =~ m{ $rx_modifier }xmsg; is_deeply \@got_modifiers, \@expected_modifiers, "'$string' -> (@got_modifiers)" ; } # end for VECTOR done_testing; __END__ ok 1 - ',20/tsra_sct,40 +?size=medium' -> (tsra tsra) ok 2 - ',30/tsra_hi +,30?size=medium' -> (rain tsra) ok 3 - ',30/rain_ +showers?size=medium' -> (rain rain) ok 4 - '' -> (bk +n) ok 5 - '' -> () 1..5 ok 6 - no warnings 1..6

Update: Moved link to haukex's article into lexical contiguity with other documentation links.

Give a man a fish:  <%-{-{-{-<

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11115107]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2020-06-01 06:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found