Nick
Nick

Reputation: 9373

Regex to split a string into 3 parts

I have an example dynamic input of information like this:

Xbox 360 (black) Elite Console 120GB (Mason City Illinois ) $200

$200 2013 North Trail Camper (RT 202. Manchester, Maine) $224/mo.

Snowmobile Bike trailers (Winthrop / Augusta) $40 Monthly

"Great Xmas Gift" XBox 360 Guitar Hero (Springfied)

I am trying to use Regex in Android to split the string into three parts:

  1. Text before XXX()
  2. Text in (XXX)
  3. Text after () XXX

Sometimes there won't be a price, the text after the location in ().

I've tried to

Pattern p = Pattern.compile("\(([^]*)\)");
Matcher m = p.matcher(title);

But I can't get matchers to work in Android. It always returns empty when I look in with matcher.group(1). I have it setup currently to look for either ( or $ and explode too give me the separate strings. But this is inaccurate and inefficient.

Any help is apperciated!

Upvotes: 3

Views: 2637

Answers (4)

maerics
maerics

Reputation: 156662

[Edit] I wouldn't use regular expressions for this problem; instead I would simply use the String#lastIndexOf(...) method to find the bounds of the last ( and ) characters and return substrings from those values:

public static String[] splitParens(String s) { 
  if (s == null) return null;
  int indexOfLastOpenParen = s.lastIndexOf('(');
  int indexOfLastCloseParen = s.lastIndexOf(')');
  return new String[] { 
    s.substring(0, indexOfLastOpenParen),
    s.substring(indexOfLastOpenParen + 1, indexOfLastCloseParen),
    s.substring(indexOfLastCloseParen + 1)
  };
} 
public static void main(String args[]) throws Exception { 
  String input[] = { 
    "Xbox 360 (black) Elite Console 120GB (Mason City Illinois ) $200",
    "$200 2013 North Trail Camper (RT 202. Manchester, Maine) $224/mo.",
    "Snowmobile Bike trailers (Winthrop / Augusta) $40 Monthly",
    "\"Great Xmas Gift\" XBox 360 Guitar Hero (Springfied)"
  };
  Pattern p = Pattern.compile("\\(([^\\)]+)\\)");
  for (String s : input) { 
    System.out.println(Arrays.asList(splitParens(s)));
  }
  // =>
  // [Xbox 360 (black) Elite Console 120GB , Mason City Illinois ,  $200]
  // [$200 2013 North Trail Camper , RT 202. Manchester, Maine,  $224/mo.]
  // [Snowmobile Bike trailers , Winthrop / Augusta,  $40 Monthly]
  // ["Great Xmas Gift" XBox 360 Guitar Hero , Springfied, ]
}

Of course, more error checking is needed (e.g. what if there is no ( or )?).

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533880

I suspect you could do this with a regular expression, but it might be simpler not to.

    String input[] = {
            "Xbox 360 (black) Elite Console 120GB (Mason City Illinois ) $200",
            "$200 2013 North Trail Camper (RT 202. Manchester, Maine) $224/mo.",
            "Snowmobile Bike trailers (Winthrop / Augusta) $40 Monthly",
            "\"Great Xmas Gift\" XBox 360 Guitar Hero (Springfied)"
    };
    for (String s : input) {
        int lastClose = s.lastIndexOf(')');
        int lastOpen = s.lastIndexOf('(', lastClose);
        System.out.println(s.substring(0, lastOpen).trim() +
                "~" + s.substring(lastOpen + 1, lastClose).trim() +
                "~" + s.substring(lastClose + 1).trim());
    }

prints

Xbox 360 (black) Elite Console 120GB~Mason City Illinois~$200
$200 2013 North Trail Camper~RT 202. Manchester, Maine~$224/mo.
Snowmobile Bike trailers~Winthrop / Augusta~$40 Monthly
"Great Xmas Gift" XBox 360 Guitar Hero~Springfied~

Upvotes: 4

Eric
Eric

Reputation: 472

If you are certain that there will always be 3 groups then this expression will work:

(.*)\((.*)\)(.*)

You can try it yourself here:

http://www.regexplanet.com/cookbook/ahJzfnJlZ2V4cGxhbmV0LWhyZHNyDgsSBlJlY2lwZRiU8y0M/index.html

Upvotes: 1

Adam P
Adam P

Reputation: 4713

Not sure if you need to use regex, but if you don't why not use String.split. Then you can use "\\(|\\)" to split the string on parentheses and then get the various sections from the created string array.

Upvotes: 1

Related Questions