Results 1 to 5 of 5

Thread: RegEx Prob. Beats me!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    Question RegEx Prob. Beats me!

    Hi,

    I just started looking at RegEx, and used one of gigemboy's to get started, which was great!

    Now though I have a specific prob and I can't seem to work it out. I have a file in this format:

    VB Code:
    1. Dim Test As String = "There once was a\nboy named Freddy \nand he\nliked his big fat Teddy\n"
    2. Dim Matches As MatchCollection = Regex.Matches(Test, "((?<!\\n)\s*[A-Za-z]+\s*)+(?=\\n)")

    "\n" is a line feed character. I want to pull out all the seperate lines. With the above code I get:

    There once was a
    nboy named Freddy
    nand he
    nliked his big fat Teddy

    ...notice the "n" at the beginning if the 2nd and onward lines.

    BTW the above is just a sample sentence for the format.

    Thx for your help. RegEx looks like being really cool. Pushing the comfort zone.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: RegEx Prob. Beats me!

    Try the below Regex sample out. One of the differences was to place each character in its own set of brackets, so it looks for a slash, and then an "n".
    VB Code:
    1. Dim Test As String = "There once was a\nboy named Freddy \nand he\nliked his big fat Teddy\n"
    2.  
    3.         'using Regex
    4.         Dim Matches As System.Text.RegularExpressions.MatchCollection = _
    5.             System.Text.RegularExpressions.Regex.Matches(Test, _
    6.             "^.*?(?=[\\][n])|(?<=[\\][n]).*?(?=[\\][n])", System.Text.RegularExpressions.RegexOptions.Singleline)
    7.         For Each Match As System.Text.RegularExpressions.Match In Matches
    8.             MessageBox.Show(Match.Value)
    9.         Next
    10.  
    11.         'using just string.repleace
    12.         Dim FinalString As String = Test.Replace("\n", Environment.NewLine)
    13.         MessageBox.Show(FinalString)
    In this case, it might be easier to just use String.Replace, however, learning Regex can be very beneficial in the future if you ever need it...

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: RegEx Prob. Beats me!

    Or use string.split on the \n

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    Re: RegEx Prob. Beats me!

    Okay, getting there. Thx.

  5. #5
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: RegEx Prob. Beats me!

    This tool has helped me with regex. It is called Expresso.

    http://www.ultrapico.com/Expresso.htm
    Last edited by Krenshau; Nov 30th, 2006 at 08:47 PM.

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