let IntAndString value =
let (|Match|_|) pattern input =
let m = Regex.Match(input, pattern) in
if m.Success then Some ([ for g in m.Groups -> g.Value ]) else None
match value with
| Match "(\d+)(\w+)" x -> Some(x)
//| Match "(\w+)" x -> Some(x)
| Match "(\d+)" x 开发者_如何学Go -> Some(x @ ["items"])
| _ -> None
So I want
10a -> 10 , a it works
100a -> 100 , a doesn't works - no idea why
100 -> 100 , items doesn't works
why 100a -> 100, a doesn't works and how can I realize it ?
If I understand your question correctly, you'd the second match clause to be used when the string contains just numbers (and the first one when there are numbers followed by letters).
The problem is that the \w
class apparentely also contains numbers (you can try it using a simple pattern), so when you match 10
against \d\w
the engine assigns 1
to \d
and 0
to \w
.
You can try something simple like this:
let IntAndString value =
match value with
| Match "([0-9]+)([^0-9]+)" x -> Some(x)
| Match "([0-9]+)" x -> Some(x @ ["items"])
| _ -> None
IntAndString "100"
Documentation for regular expressions should explain the behavior you encountered.
Try something like Match "(\d+)(\w*)" 'cause the + means 1 times or more. the * means 0 times or more.
精彩评论