Results 1 to 3 of 3

Thread: Help with RegEx

  1. #1

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Help with RegEx

    Hi,

    I have a set of sentences and need to weed out the bad ones and then extract the left part (the sentence) and the right part (number of minutes).


    A is on phone from last 15min
    B is on phone with A from last 15min
    D is ready to talk to C now
    D is 1min closer to talk to C now
    E is waiting for D forever
    F is waiting for G to pick up phone from last -12min
    F is waiting for G to pick up phone from last 42min
    F is waiting for G to pick up phone from last min
    F is waiting for G to pick up phone from last -12 min
    F is waiting for G to pick up phone from last 0 min
    G will speak to F later


    Rules
    • The sentence must end with either the number of minutes (i.e. "42min") or the word "now"
    • There must be at least 1 space before the number of minutes (i.e. "WHAT EVER THE SENTENCE IS 42min")
    • There cannot be any space between the the number of minutes and the word "min" (i.e "42min" is ok, "42 min" is not ok)
    • The number of minutes must be more than 5
    • Apart from the "42min" at the end, the rest of the sentence should not contain any numbers

    So from the above set,

    YUPS : A is on phone from last 15min
    YUPS : B is on phone with A from last 15min
    YUPS : D is ready to talk to C now
    NOPE : D is 1min closer to talk to C now
    NOPE : E is waiting for D forever
    NOPE : F is waiting for G to pick up phone from last -12min
    NOPE : F is waiting for G to pick up phone from last 42 min
    NOPE : F is waiting for G to pick up phone from last min
    NOPE : F is waiting for G to pick up phone from last -12 min
    NOPE : F is waiting for G to pick up phone from last 0min
    NOPE : F is waiting for G to pick up phone from last 0 min
    NOPE : G will speak to F later


    This is what I have come up with but it is not giving me the desired result and I am stuck. Any help is appreciated.

    http://www.regexr.com/39dj6

    Cheers

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Help with RegEx

    Not a Regex expert, but I like to play.

    Here's the horrible pattern I came up with. Plug it into your Regex site link and see if it works for you.

    (^[^\d\r\n]+?)( +)(((([6789])|(\d{2,}))min\r?$)|(now\r?$))


    Or here's a VB solution. I've given each group a name to make it easier to retrieve the required data. The input sentences are slightly different from yours, in order to show that time values less than 6 are disallowed:
    Code:
    Dim sentences() As String = {"A is on phone from last 5min",
                                 "B is on phone with A from last 7min",
                                 "D is ready to talk to C now",
                                 "D is 1min closer to talk to C now",
                                 "E is waiting for D forever",
                                 "F is waiting for G to pick up phone from last -12min",
                                 "F is waiting for G to pick up phone from last 42min",
                                 "F is waiting for G to pick up phone from last min",
                                 "F is waiting for G to pick up phone from last -12 min",
                                 "F is waiting for G to pick up phone from last 0 min",
                                 "G will speak to F later"}
    
    '   Join array of lines into a single string
    '   each line separated by /r/n (standard vb line break)
    '   (will also work on each single sentence without joining)
    Dim input As String = String.Join(vbCrLf, sentences)
    
    Dim pattern As String = "(?<LeftPart>^[^\d\r\n]+?)(?<SpacesBetween> +)(?<RightPart>((([6789])|(\d{2,}))min\r?$)|(now\r?$))"
    
    
    Dim rx As New Regex(pattern, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
    Dim matches As MatchCollection = rx.Matches(input)
    
    For Each m As Match In matches
            MessageBox.Show(String.Format("{1}{0}{2}{0}{3}", vbNewLine,
                                                    m.Value,
                                                    m.Groups("LeftPart"),
                                                    m.Groups("RightPart")))
    
    Next
    So from the following set:

    A is on phone from last 5min
    B is on phone with A from last 7min
    D is ready to talk to C now
    D is 1min closer to talk to C now
    E is waiting for D forever
    F is waiting for G to pick up phone from last -12min
    F is waiting for G to pick up phone from last 42min
    F is waiting for G to pick up phone from last min
    F is waiting for G to pick up phone from last -12 min
    F is waiting for G to pick up phone from last 0 min
    G will speak to F later

    the code matches:

    B is on phone with A from last 7min
    D is ready to talk to C now
    F is waiting for G to pick up phone from last 42min


    I'd be interested to know what the experts come up with.

  3. #3

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Re: Help with RegEx

    Quote Originally Posted by Inferrd View Post
    Not a Regex expert, but I like to play.
    Here's the horrible pattern I came up with. Plug it into your Regex site link and see if it works for you.

    (^[^\d\r\n]+?)( +)(((([6789])|(\d{2,}))min\r?$)|(now\r?$))
    Hey thanks man. Appreciate it. I think I can safely say, RegEx are my weak point.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width