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:
So from the following set: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
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.




Reply With Quote