I have a string that I know will match one (and only one) of three regexes. I want to try each regex in turn until a match is found. For two of the regexes it is sufficient to know that there is a match. The third regex has a capture group and returns an integer.
I have an active pattern for regex:
let (|Regex|_|) pattern input =
let m = Regex.Match(input, pattern)
if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
else None
I’m new to F# and struggling for an idiomatic way to do this. I don’t really want to make a convoluted if-then-else expression,
Any help would be really appreciated.
You could create three active patterns that match the three regex patterns you want to match, then just match for them in the order you want. All the if/else will then contained in the active patterns. Having the if/else in the guards may look perfectly fine though, don’t be scared of using if/else theres nothing wrong with using them.