, , .
, , , , if/then/else:
let y x =
if x.Contains("hi") then "HELLO"
elif x.Contains("hello") then "HI"
else x
Contains, @Tomas Petricek, , , ,
open System.Text.RegularExpressions
let (|InterpretedMatch|_|) pattern input =
if input = null then None
else
let m = Regex.Match(input, pattern)
if m.Success then Some [for x in m.Groups -> x]
else None
let (|CompiledMatch|_|) pattern input =
if input = null then None
else
let m = Regex.Match(input, pattern, RegexOptions.Compiled)
if m.Success then Some [for x in m.Groups -> x]
else None
:
let y = function // shorthand for let y x = match x with ...
| CompiledMatch @"hi" _ -> "HELLO"
| CompiledMatch @"hello" _ -> "HI"
| x -> x
, : Contains, StartsWith, EndsWith, Equals :
let y = function
| CompiledMatch @"^hi$" _ -> "Equals"
| CompiledMatch @"^hi" _ -> "StartsWith"
| CompiledMatch @"hi$" _ -> "EndsWith"
| CompiledMatch @"leadinghiending" _ -> "Beyond"
| CompiledMatch @"hi" _ -> "Contains"
| x -> x
(note that the literal lines introduced by @ are not really needed for any of these regular expression examples, but as a rule, I always use them since you need them more often than regular regular expressions).
source
share