How do you express the computation that, given a list of regular expressions, perform a replacement until one of them is satisfied.
For instance:
keys = {"x_millisec", "y", "z_milliseconds", "w_somethingelse", "a_Millisecs"}
pattern = ... (* it defines the named group `prefix' *)
With[ {key = ..., value = ... },
Which[
StringMatchQ[ key, pattern, IgnoreCase -> True],
StringReplace[ key, {
(* remove the suffix `_millisecs' *)
pattern :> StringTake[ prefix, StringLength[prefix] - 1]
}, IgnoreCase -> True ] -> Quantity[ value, "Milliseconds"] ,
...
True, key -> value]
]
] & /@ keys
This performs the replacement for a single pattern. Now I have a list of patterns for different units, such as seconds, millisecs, microsecs, nanosecs. I would to like to test the same string, `key', against each of those pattern, until one of them is satisfied. Is there a simpler way to proceed rather than extending the above redundant code?
SelectFirst
was designed to do. $\endgroup$