0

我想提取一个位于几个方括号内的模式。它们通常看起来像这样:
this is what I want[Pat1 France�s Wal-mart ] and this is [Pat2 boy-o-boy ]

我的正则表达式看起来像这样:

Pattern search = Pattern.compile("\\[[\\w.\\.\\-\\s]+]");

如何合并特殊字符或我应该在模式中更改什么以提取可能出现在这些大括号中的任何字符?

我想以某种方式提取 [Pat1 France's Wal-mart] 和 [Pat2 boy-o-boy ]。我可以提取 pat2 部分。感谢您的任何建议。

4

2 回答 2

1

你可以使用这个正则表达式

\\[.*?\\]
于 2013-01-10T15:15:45.943 回答
1

只需使用.与任何字符匹配的。

 String s= "[Pat1 France�s Wal-mart ]";
        Pattern p = Pattern.compile("\\[.*?\\]");
        Matcher m = p.matcher(s);
        System.out.println(m.find() + " " + m.group());
于 2013-01-10T15:16:07.080 回答