Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Regex to Array lookup question

by AnomalousMonk (Archbishop)
on Apr 05, 2020 at 22:59 UTC ( [id://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 = ( [ 'https://api.weather.gov/icons/land/day/tsra_sct,20/tsra_sct,40?si +ze=medium', 'tsra', 'tsra', ], [ 'https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,30 +?size=medium', 'rain', 'tsra', ], [ 'https://api.weather.gov/icons/land/night/rain_showers,30/rain_sho +wers?size=medium', 'rain', 'rain', ], [ 'https://api.weather.gov/icons/land/day/bkn?size=medium', 'bkn', ] +, [ 'https://api.weather.gov/icons/land/day/size=medium', ], ); 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 - 'https://api.weather.gov/icons/land/day/tsra_sct,20/tsra_sct,40 +?size=medium' -> (tsra tsra) ok 2 - 'https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi +,30?size=medium' -> (rain tsra) ok 3 - 'https://api.weather.gov/icons/land/night/rain_showers,30/rain_ +showers?size=medium' -> (rain rain) ok 4 - 'https://api.weather.gov/icons/land/day/bkn?size=medium' -> (bk +n) ok 5 - 'https://api.weather.gov/icons/land/day/size=medium' -> () 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-26 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found