2017-05-08 49 views
1

如何使用函數式方法來執行正則表達式?目前我希望用戶輸入一個輸入,即使他們用大寫字母輸入,它仍會給出迴應?我不確定如何實現這一點。正則表達式f#

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" 
+0

我會檢查MSDN的dot-net正則表達式示例,然後專門查看它們是否具有f#選項卡。不確定,但網絡正則表達式應該是所有的語言(或他們說)。 – sln

回答

1

您可以像使用C#或VB.NET(或任何其他.NET語言)一樣使用F#中的正則表達式。 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 

你也可以「緩存」正則表達式通過創建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