Regex problem.

Discussion in 'Java' started by X. Lee, Nov 5, 2004.

  1. X. Lee

    X. Lee Guest

    Hello, I am in need of some regex assistance..

    In my project, I need to parse a String and replace everything within
    brackets with the String "xx".

    I thought this would be pretty simple, using the Pattern "(\[.+\])".

    this pattern works fine if there is only one set of brackets in my
    String, however it fails if I have multiple bracket sets because it
    only seems to consider the very first and very last brackets found.

    e.g.:

    "This is my [test] String" becomes "This is my [xx] String" <-- fine

    but "This is [my] other [test] String" also becomes "This is my [xx]
    String" instead of "This is [xx] other [xx] String" <-- not fine

    So the matcher finds "[my] other [test]" instead of finding [my] and
    [test] separately.

    How can I make it find each set of brackets separately?
    X. Lee, Nov 5, 2004
    #1
    1. Advertising

  2. X. Lee

    Guest

    It does work if you reduce the narrow down the intermediate
    characters...

    System.out.println("This is [my] other [test]
    String".replaceAll("[\\[][a-zA-Z0-9]+[\\]]", "xx"));

    does print "This is xx other xx String". The ']' seems to be included
    in ".+".

    Just a thought.
    -Siplin
    , Nov 5, 2004
    #2
    1. Advertising

  3. try "(\[[^\]+])"
    means: first "[", then sequence of chars except "]" and last "]"

    the problem is that "+" and "*" are greedy, they take as much as they can.
    There is a possipility to use the shortest match with an "?", but I am not
    sure about the correct syntax.

    bye,
    feri
    Ferenc Hechler, Nov 5, 2004
    #3
  4. X. Lee

    Alan Moore Guest

    On Fri, 5 Nov 2004 23:58:32 +0100, "Ferenc Hechler"
    <> wrote:

    > the problem is that "+" and "*" are greedy, they take as much as they can.
    > There is a possipility to use the shortest match with an "?", but I am not
    > sure about the correct syntax.


    You just add the '?' after the '*' or '+':

    str = str.replaceAll("\\[.*?\\]", "[xx]");
    Alan Moore, Nov 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    688
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,604
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    589
  4. Xah Lee
    Replies:
    1
    Views:
    927
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    726
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page