Reputation: 9373
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:
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
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
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
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
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