|
-
Jan 2nd, 2009, 02:03 PM
#1
Thread Starter
Sleep mode
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 ?
Last edited by Pirate; Jan 4th, 2009 at 04:50 AM.
-
Jan 3rd, 2009, 01:01 AM
#2
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
Last edited by Harsh Gupta; Jan 3rd, 2009 at 01:04 AM.
-
Jan 3rd, 2009, 11:53 AM
#3
Thread Starter
Sleep mode
Re: Regex , find match and get text just before the next match
Thanks...it's working well.
-
Jan 3rd, 2009, 02:10 PM
#4
Re: Regex , find match and get text just before the next match
No probs.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|