Results 1 to 7 of 7

Thread: [RESOLVED] [2008] Need RegEx patterns again... :S

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] [2008] Need RegEx patterns again... :S

    Hi,

    I yet again ask the help of you people for a few simple regex patterns. I just don't understand it at all. I've read tutorials, examples, but then I still can't get it. I asked for an example a few times on this forum, and while the result works perfectly fine each time, I still can't understand why it does...


    So I can't help it, but I'm going to need your help again


    I need three patterns:
    The bold numbers are the ones I need to extract. The rest is just to help find the correct strings, I don't need them after that.

    Pattern one:
    1. The string to search for starts with a pre-defined string (let's assume it is this_string for simplicity).
    2. This string can in rare cases be preceded by spaces or tabs, but not by any other characters.
    3. This string is followed by an arbitrary number of spaces or tabs.
    4. Then, it is followed by a number (between 1 and 8, so only one digit).
    5. Followed again by an arbitrary number of spaces or tabs.
    6. Then, it ends in a string between quotes (let's assume it is "end_of_string") (can contain spaces, or any other character except quotes, which denote the end of the string).
    (7. It can be followed by another series of spaces/tabs or even comments initiated by "//")

    So I need the number (in 4.) and the last string between quotes.

    Example:
    Code:
    // A comment
    this_string   1  "this is the end of the string, I need to extract this"   //comment
    Should return: the number 1, and the string this is the end of the string, I need to extract this.


    Pattern two:
    1. The string to search for again starts with an arbitrary number of spaces or tabs.
    2. Then it is followed by a pre-defined string, let's assume this_string again.
    3. Another arbitrary number of spaces/tabs
    4. Another string, between quotes (can contain spaces or any other character except quotes, which denote the end of the string).
    5. Followed again by spaces, tabs or comments ("// ...")

    So basically the same as pattern one except for the number in between, and it should also return the pre-defined string (so I can match the pre-defined strings with the end_of_strings)

    Example:
    Code:
    {
        map            "get me!"
        longname     "and me too!"   // comment
    }
    Should return: map & "get me!" and longname & "and me too!" (with or without quotes doesn't really matter as long as it's consistent).
    (Can it return an array for example? Where ar(0) = "map" and ar(1) = "get me!" for example? Basically I need to be able to match the 'map' with 'get me!' so I know which belongs to which...)

    Pattern three:
    Basically the same as pattern two, except this time the final string to obtain is a number (integer), and is not contained in quotes.

    Example:
    Code:
    {
        map            "get me!"
        longname     "and me too!"   // comment
        timelimit       31   //comment
        mapposition_x     780
    }
    Should return: timelimit & 31, and mapposition_x and 780.
    So again I need both the pre-defined strings (timelimit and mapposition_x) and the numbers, so I can know which belongs to which...)


    I'm sorry if this is asking for too much lol... I can assure you it's not homework (I don't even study programming), it would just make my life so much easier. I can parse the files manually by just looping through each line but the number of options to check for (spaces, tabs, comments etc) makes it just so error prone that I'd rather use RegEx... The problem ofcourse being I don't understand it


    Thanks in advance for any help!

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: [2008] Need RegEx patterns again... :S

    For the first one, I'll plump for

    Code:
            Dim s As String = "this_string      5    "" 212 2 33 f dfd df"""
            Dim r As New Regex("(?<=[ \t]?this_string[ \t]*)[1-8](?=[ \t]*"".*?"")", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
            Dim matches As MatchCollection = r.Matches(s)
            For Each m As Match In matches
                MessageBox.Show(m.ToString)
            Next
    This for the second;

    Code:
           Dim r As New Regex("(?<=[ \t]?this_string[ \t]*"").*?(?="")", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
    Last edited by Bulldog; Mar 13th, 2009 at 01:58 PM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Need RegEx patterns again... :S

    Quote Originally Posted by Bulldog
    For the first one, I'll plump for

    Code:
            Dim s As String = "this_string      5    "" 212 2 33 f dfd df"""
            Dim r As New Regex("(?<=[ \t]?this_string[ \t]*)[1-8](?=[ \t]*"".*?"")", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
            Dim matches As MatchCollection = r.Matches(s)
            For Each m As Match In matches
                MessageBox.Show(m.ToString)
            Next
    Thanks, but it seems to only return the number, not the corresponding text...?


    EDIT
    Second one seems to work fine, thanks!

    Still looking for 1) though... I think I can figure out the 3rd from the 2nd now, let me try

    Meh, I tried this for 3) but doesn't seem to work, it's just returning spaces, not the required integers:
    Code:
    Dim r As New Regex("(?<=[ \t]?this_string[ \t]*).*?(?=)", ...)

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: [2008] Need RegEx patterns again... :S

    Try these
    Code:
    Dim pattern1 As String = "(?<=\s*\w+\s+)\d\s+.*(?=""\s*//\s*comment)"
    Dim pattern2 As String = "(?<=\s*\w+\s+"").*(?=""\s*)"
    Dim pattern3 As String = "(?<=\s*)[a-z_]+\s+\d+(?=\s*)"
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Need RegEx patterns again... :S

    Quote Originally Posted by stanav
    Try these
    Code:
    Dim pattern1 As String = "(?<=\s*\w+\s+)\d\s+.*(?=""\s*//\s*comment)"
    Dim pattern2 As String = "(?<=\s*\w+\s+"").*(?=""\s*)"
    Dim pattern3 As String = "(?<=\s*)[a-z_]+\s+\d+(?=\s*)"
    Only the third seems to work.
    The first one seems to assume there is always a "//comment" or something..? That's not the case, there could be a comment, but usually there isn't.

    The second one simply returns the whole text.

    Thanks though!


    So I still need a pattern for the first one...

  6. #6
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: [2008] Need RegEx patterns again... :S

    To get the last string;

    Code:
            Dim s As String = "this_string      5    "" 212 2 33 f dfd df"""
            Dim r As New Regex("(?<=[ \t]?this_string[ \t]*[1-8][ \t]*"").*?(?="")", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
            Dim matches As MatchCollection = r.Matches(s)
            For Each m As Match In matches
                MessageBox.Show(m.ToString)
            Next
    The other pattern I posted gets the number, or if you want both together then;

    Code:
            Dim s As String = "this_string      5    "" 212 2 33 f dfd df"""
            Dim r As New Regex("(?<=[ \t]?this_string[ \t]*)[1-8][ \t]*"".*?(?="")", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
            Dim matches As MatchCollection = r.Matches(s)
            For Each m As Match In matches
                MessageBox.Show(m.ToString)
            Next


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  7. #7

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