Regex , find match and get text just before the next match
Say i have this text (from dictionary) :
knife n 1: edge tool used as a cutting instrument; has a pointed blade with a sharp edge and a handle 2: a weapon with a handle and blade with a sharp point 31: any long thin projection that is transient; "tongues of flame licked at the walls"; "rifles exploded quick knives of fire into the dark" [syn: {tongue}] v : use a knife on; "The victim was knifed to death" [syn: {stab}] [also: {knives} (pl)] .
I want to use regex to be able to put every definition on new line . Like so :
knife n
1: edge tool used as a cutting instrument; has a pointed blade with a sharp edge and a handle
2: a weapon with a handle and blade with a sharp point
3: any long thin projection that is transient; "tongues of flame licked at the walls"; "rifles exploded quick knives of fire into the dark" [syn: {tongue}] v : use a knife on; "The victim was knifed to death" [syn: {stab}] [also: {knives} (pl)]
I used this pattern "\s[1-9]+\x3A\s" to find number of definitions but i don't know how to select the match " n: " along with the word definition (just exactly before the second match occurs).
Any regex guru ?
Re: Regex , find match and get text just before the next match
There is a Replace function in RegEx library of .Net. You may need to pass a custom MatchEvaluator delegate to it for your issue. Please check the following code, but I am not really sure if there is another neat and clean method.
VB.Net Code:
Dim myDelegate As New System.Text.RegularExpressions.MatchEvaluator(AddressOf MyMatchEvaluator)
Dim input As String = "knife n 1: edge tool used as a cutting instrument; has a pointed blade with a sharp edge and a handle 2: a weapon with a handle and blade with a sharp point 31: any long thin projection that is transient; ""tongues of flame licked at the walls""; ""rifles exploded quick knives of fire into the dark"" [syn: {tongue}] v : use a knife on; ""The victim was knifed to death"" [syn: {stab}] [also: {knives} (pl)] ."
Dim pattern As String = "\s[1-9]+\x3a\s"
Dim regex As New System.Text.RegularExpressions.Regex(pattern)
Console.Write(regex.Replace(input, myDelegate))
...
Private Function MyMatchEvaluator(ByVal match As System.Text.RegularExpressions.Match) As String
Return Environment.NewLine + match.Groups(0).Value
End Function
Re: Regex , find match and get text just before the next match
Thanks...it's working well.
Re: Regex , find match and get text just before the next match