#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    13
    Rep Power
    0

    Finding string between [ and ]


    I want to use Regex to find any string that starts with [ and ends with ]

    How can I do this?

    Thanks,

    Jon
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    13
    Rep Power
    0
    Thanks for the quick reply! I tried it in UltraEdit and it didn't replace the strings. Any other ideas or is the problem with UltraEdit?

    My data looks like this:

    [Event "?"]
    [Site "London"]
    [Date "1794.??.??"]
    [Round "?"]
    [White "Atwood, George"]
    [Black "Philidor, Francois Andre Dani"]
    [Result "1-0"]
    [ECO "B21"]
    1. e4 c5 2. f4 Nc6 3. Nf3 e6 4. c3 d5 5. e5 f5 6. d4 Nh6 7. h3 Qb6 8. b3 Bd7
    9. Be3 Nf7 10. Qd2 O-O-O 11. Qf2 cxd4 12. Nxd4 Nxd4 13. Bxd4 Qc6 14. Nd2 b6
    15. a4 Bc5 16. Bb5 Bxd4 17. Qxd4 Qc5 18. Nf3 Bxb5 19. Qxc5+ bxc5 20. axb5 Kb7
    21. Ke2 Ra8 22. Ra6 Rhe8 23. Rd1 Nd8 24. Ne1 c4 25. bxc4 dxc4 26. Rd7+ Kc8
    27. Rxg7 Rb8 28. Raxa7 Rxb5 29. Rac7+ Kb8 30. Rxc4 Rb7 31. Rb4 Rxb4 32. cxb4
    Nc6 33. Nd3 Re7 34. Rxe7 Nxe7 35. Nc5 Ng6 36. Nxe6 Kc8 37. Ke3 Kd7 38. Nd4
    Ne7 39. g4 Ke8 40. g5 1-0
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    Ishnid's pattern was missing an escape:
    Code:
    (\[[^\]]+\])
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Originally Posted by ManiacDan
    Ishnid's pattern was missing an escape
    Ah. The OP hadn't initially stated which regexp implementation was being used. I tested it in Perl, which doesn't require an escape in this case.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,907
    Rep Power
    6351
    Perl allows for "[^]]" just like that? God perl is weird.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0
    need help with a regular expression that matches a string enclosed with square brackets and can contain any characters except ] character , but matches if ] appears in pair like ]].
    For e.g.
    The regex should not match the following:

    [Hello]]

    but if the string is something like

    [Hello]]], it should match.

    Thanks in advance guys.
  14. #8
  15. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    This seems to do it:
    Code:
    /\[(?:[^]]|]])+](?!])/
    @ManiacDan: PHP allows [^]] too, as does Java.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0
    Great!! It worked.
    Thanks a lot.

    Sujen

IMN logo majestic logo threadwatch logo seochat tools logo