Results 1 to 4 of 4

Thread: Regex , find match and get text just before the next match

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Resolved 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.

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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:
    1. Dim myDelegate As New System.Text.RegularExpressions.MatchEvaluator(AddressOf MyMatchEvaluator)
    2.  
    3.         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)] ."
    4.         Dim pattern As String = "\s[1-9]+\x3a\s"
    5.         Dim regex As New System.Text.RegularExpressions.Regex(pattern)
    6.  
    7.         Console.Write(regex.Replace(input, myDelegate))
    8.  
    9. ...
    10.  
    11.     Private Function MyMatchEvaluator(ByVal match As System.Text.RegularExpressions.Match) As String
    12.         Return Environment.NewLine + match.Groups(0).Value
    13.     End Function
    Last edited by Harsh Gupta; Jan 3rd, 2009 at 01:04 AM.
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Regex , find match and get text just before the next match

    Thanks...it's working well.

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Regex , find match and get text just before the next match

    No probs.
    Show Appreciation. Rate Posts.

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