Results 1 to 5 of 5

Thread: Simple regex pattern matching more than I want....

  1. #1

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

    Simple regex pattern matching more than I want....

    Hi,

    I need an extremely simple regex pattern that matches anything within square brackets [].

    Example:
    Code:
    This is a [test] text with [some] [examples].
    Required results: [test], [some], [examples].

    I don't want to get rid of the brackets, they should be included in the result (that should make it easier, right?)

    If possible I'd like to exlude results that have whitespace in them, so things like [this string] should not be matched. If that gets too hard (probably not, but hey I can't even manage this :S) then I don't really mind, I can always check if the result contains whitespace myself.


    I tried a lot of examples I could find online and ones that I could think of myself but none work. Note: I've been testing with the Visual Studio Find dialog (checking to use regular expressions obviously), maybe that's where the problem lies, because I don't really understand why some of these don't work...


    Anyway, first thing I tried was simple: match the two brackets and one or more characters in between (that should be a +, right?)
    Code:
    \[+\]
    No results in a file containing multiple of [these] strings...

    Then I tried like a dozen others, some of which according to VS contained errors and some failed to match anything.


    The closest I got was using
    Code:
    \[.*\]
    But, that also matches strings that span multiple 'parts'.
    Example:
    Code:
    Hello, [my] name [is] Nick
    here it also matches '[my] name [is]', which is not what I'm looking for....


    I don't understand why I suck at regex so much but I just can't find the right pattern, lol... Any help?

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Simple regex pattern matching more than I want....

    Is 'simple regex pattern' an oxymoron?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Simple regex pattern matching more than I want....

    try this. the ? character can be used to specify a lazy search, where it'll return the shortest possible match. the + character specifies 1 character or more, whereas the * character specifies 0 characters or more.

    vb Code:
    1. Dim rx As New Regex("\[[A-Za-z]+?\]")

  4. #4
    Lively Member
    Join Date
    May 2011
    Posts
    96

    Re: Simple regex pattern matching more than I want....

    Code:
    \[[^\]]\S+\]
    Excludes matches with a space.
    Last edited by buggyboy; Jul 2nd, 2011 at 03:25 AM.

  5. #5

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

    Re: Simple regex pattern matching more than I want....

    When I try both of these in the VS editor (Find dialog with regular expressions) they can't find any matches... Is the VS Find regex bugged or something, or do I need to delimit the expressions or something???

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