2012-12-19 89 views
2

我的信息等这样的一个例子的动态输入:正则表达式到一个字符串分割成3份

的Xbox 360(黑色)精英控制台120GB(梅森城伊利诺伊)$ 200

$ 200 2013北径露营者(RT 202.曼彻斯特,缅因州)224美元/月。

雪地摩托自行车拖车(温斯罗普/奥古斯塔)$ 40每月

“伟大的圣诞礼物” 的Xbox 360吉他英雄(Springfied)

我想使用正则表达式在Android中的字符串分割成三个部分:

  1. XXX()
  2. 文本中(XXX)
  3. 文本之前的文本after()XXX

有时候不会有价格,在()后面的文字位置。

我试图

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

但我不能匹配器在Android中运行。当我用matcher.group(1)查看时,它总是返回空白。我把它设置当前寻找任何(或$和爆炸太给我单独的字符串。但是,这是不准确的和低效的。

任何帮助apperciated!

+1

我假设你要包括'(梅森城伊利诺伊州)',但不包括'(黑色)'? –

+0

是的,这是我遇到的问题之一 – Nick

+0

我怀疑你只想解析最后一个'()'。 –

回答

4

我怀疑你可以用正则表达式来做到这一点,但它可能会更简单。

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()); 
    } 

打印

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~ 
+1

Ack,我刚刚更新了我的答案,做同样的事情!好主意 ;-) – maerics

1

不知道你是否需要使用正则表达式,但如果你不为什么不使用String.split。然后,您可以使用"\\(|\\)"将括号中的字符串拆分,然后从创建的字符串数组中获取各个部分。

+0

这是我的原始方法,但正如您在第一个项目中看到的那样,它有两套parens和字符串分割不能很好地处理。 – Nick

1

[编辑]我不会对这个问题使用正则表达式;相反,我会简单地使用String#lastIndexOf(...)方法来找到最后()字符的边界,并返回子从这些值:

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, ] 
} 

当然,需要更多的错误检查(例如,如果没有()?)。

+0

它需要拆分最后一个(),而不是第一个。 –

+0

是的,第一行分裂,所以黑色将是位置和精英控制台... $ 200将是我试图避免的价格 – Nick

+0

@Nick:明白了;我只是更新了我的答案,但看起来PeterLawrey打败了我! – maerics

相关问题