Results 1 to 5 of 5

Thread: [RESOLVED] Regex question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Resolved [RESOLVED] Regex question

    I was trying to write a regular expression to look for the following conditions and am having trouble understanding how to do it. It's for reading an INI file. I know there is code out there to do this but would like to understand how to do it myself.

    Here are the conditions for one line read from a text file:

    1. contains one and only one '[' AND one and only one ']'
    2. returns the data between the brackets
    3. provides some information that allows me to trap an error if it's not a properly formatted section heading. ([[text], text], etc)

    Here's what I was trying to use "\[\(.*\)\]" with regex.matches.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Regex question

    "\[\(.*\)\]" this would return anything that is between "[" and "]" AND between parenthesis as you escaped the parenthesis with the backslash character.

    "\[(.*)\]" This would return anything that is between "[" and "]" without mandatory parenthesis.

    does that help ? I'm not sure I completely understand what you want tho.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: Regex question

    Actually that's returning the brackets as well. I'd like the data between them. I'd also like to find out if there are multiple brackets "[[text]" or single brackets "[text".

    Mostly I'm doing this to try and learn more about regular expressions which I've avoided doing for a long time now.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  4. #4
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Regex question

    Code:
    (?<=\[).*(?=\](?!\]))
    This regular expression is using Positive Look Behind and Positive Look Ahead AND Negative Look Ahead. Basically it catches anything that follows the first occurrence found of a "[" and that precedes the last occurrence found of a "]" so if you run it against "[text]" it will return "text", if you run it against "[[text]" it will return "[text" and if you run it against "[text]]" it will return "text]".

    It works well as long as the brackets are all together like "[[[" and "]]]"

    So you could split a little bit like this for a better understanding
    Code:
    (?<=\[) This part says "preceded by an opening bracket"
    .* This one simply sais "catch anything"
    (?=\](?!\])) This one is the tricky one it says "followed by a closing bracket which is NOT followed by a closing bracket
    there are a couple of links in my signature to get you started. There's also this site I like to use to test out my Regular Expressions
    Last edited by stlaural; May 26th, 2010 at 09:33 AM.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: Regex question

    Thanks Alex. I also am reading the links in your signature. Very helpful, I should have looked there first.

    Appreciate the help and the explanation of what the expression characters are doing.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

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