正则表达式f#

时间:2017-05-08 20:21:56

标签: regex f# functional-programming

如何使用功能方法进行正则表达式?目前我希望用户输入输入,即使他们以大写字母输入,它仍会给出回复?我不确定如何实现这一点。

open System
open System.Drawing 
open System.Windows.Forms

let (|Hello|Bye|None|) input =
match input with
    | "hello" | "hi" | "morning" 
            -> Hello
    | "Goodbye" | "bye" | "go"
            -> Bye
    | _     
            -> None

   let rand = new Random()

   let hello_response () =
   let n = rand.Next(10) 
   match n with
    | 0 -> "How do you do."
    | 1 -> "Is nice talking to you."
    | 2 -> "Tell me something new."
    | 3 -> "Nice to meet you."
    | 4 -> "My pleasure."
    | 5 -> "Hi."
    | 6 -> "Hello."
    | 7 -> "Good day."
    | 8 -> "Salutation!"
    | 9 -> "Welcome!"


    let good_bye_response () =
    let n = rand.Next(10) 
    match n with
    | 0 -> "Talk to you soon."
    | 1 -> "It was nice talking to you."
    | 2 -> "Good bye."
    | 3 -> "Stay a bit longer."
    | 4 -> "Adios amigo."
    | 5 -> "Bye."
    | 6 -> "Adios."
    | 7 -> "See you."
    | 8 -> "Please don't go"
    | 9 -> "Why are you leaving me alone?"

    let none_response (str:string) =
    let n = rand.Next(10) 
    match n with
    | 0 -> "Maybe."
    | 1 -> "Perhaps " + str 
    | 2 -> "Yes."
    | 3 -> "Ah!"
    | 4 -> "Whatever." 
    | 5 -> "Sorry, the chat closed unexpectedly. What was your last 
    question?"
    | 6 -> "Where were we? I losed track of the conversation."
    | 7 -> "Very interesting"
    | 8 -> "Wow!"
    | 9 -> "Mmmmmm!"

    let rec response (token: string) (str: string) =
    match token with
    | Hello
        -> hello_response ()
    | Bye
        -> good_bye_response ()
    | "" 
        -> none_response str
    | None when (str.IndexOf(" ") > 0) 
        -> response (str.Substring(0,str.IndexOf(" "))) 
    (str.Substring(str.IndexOf(" ")+1))
    | None when (str.IndexOf(" ") < 0) 
        -> response str ""
    | None 
        -> str

    let marketbot_resp (str: string) =
    if (str.IndexOf(" ") > 0) then
    response (str.Substring(0,str.IndexOf(" "))) 
    (str.Substring(str.IndexOf(" ")+1)) + "\n"
    else
    response str "" + "\n"

1 个答案:

答案 0 :(得分:1)

您可以使用F#中的正则表达式,就像使用C#或VB.NET(或任何其他.NET语言)一样。 MSDN提供了有关该主题的大量文档。 Check it out

根类为System.Text.RegularExpressions.Regex。最简单的匹配方法是通过IsMatch静态方法:

let (|Hello|Bye|None|) input =
    if Regex.IsMatch( input, "(?i)hello|hi|morning" ) then Hello
    elif Regex.IsMatch( input, "(?i)goodbye|bye|go" ) then Bye
    else None

你也可以&#34;缓存&#34;通过创建Regex的实例并重用它来使用正则表达式。这将为您节省一点性能:

let (|Hello|Bye|None|) =
    let hello = Regex "(?i)hello|hi|morning"
    let bye = Regex "(?i)goodbye|bye|go"
    fun input ->
        if hello.IsMatch input then Hello
        elif bye.IsMatch input then Bye
        else None

然而,对于这个特殊的任务,我认为正则表达式是过度的。我会在匹配之前将字符串转换为小写:

let (|Hello|Bye|None|) (input: string) =
    match input.ToLower() with
    | "hello" | "hi" | "morning" 
            -> Hello
    | "goodbye" | "bye" | "go"
            -> Bye
    | _     
            -> None