Java 不接受正则表达式

Java 不接受正则表达式,java,regex,Java,Regex,我已经实现了计算文本中单词出现次数的代码。但是,由于某些原因,我的正则表达式不被接受,我得到以下错误: 线程“main”java.util.regex.PatternSyntaxException中的异常:索引12附近的未关闭字符类 我的代码是: import java.util.*; 公共类统计词的发生率{ /** * @param args the command line arguments */ public static void main(String[] args) {

我已经实现了计算文本中单词出现次数的代码。但是,由于某些原因,我的正则表达式不被接受,我得到以下错误:
线程“main”java.util.regex.PatternSyntaxException中的异常:索引12附近的未关闭字符类

我的代码是:

import java.util.*;
公共类统计词的发生率{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    char lf = '\n';

String text = "It was the best of times, it was the worst of times," + 
lf +
"it was the age of wisdom, it was the age of foolishness," + 
lf +
"it was the epoch of belief, it was the epoch of incredulity," + 
lf +
"it was the season of Light, it was the season of Darkness," + 
lf +
"it was the spring of hope, it was the winter of despair," + 
lf +
"we had everything before us, we had nothing before us," + 
lf +
"we were all going direct to Heaven, we were all going direct" + 
lf +
"the other way--in short, the period was so far like the present" + 
lf +
"period, that some of its noisiest authorities insisted on its" + 
lf +
"being received, for good or for evil, in the superlative degree" + 
lf +
"of comparison only." + 
lf +
"There were a king with a large jaw and a queen with a plain face," + 
lf +
"on the throne of England; there were a king with a large jaw and" + 
lf +
"a queen with a fair face, on the throne of France.  In both" + 
lf +
"countries it was clearer than crystal to the lords of the State" + 
lf +
"preserves of loaves and fishes, that things in general were" + 
lf +
"settled for ever";

    TreeMap<String, Integer> map = new TreeMap<String, Integer>();
    String[] words = text.split("[\n\t\r.,;:!?(){");
    for(int i = 0; i < words.length; i++){
        String key = words[i].toLowerCase();

        if(key.length() > 0) {
            if(map.get(key) == null){
                map.put(key, 1);
            }
            else{
                int value = map.get(key);
                value++;
                map.put(key, value);
            }
        }
    }

    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();

    //Get key and value from each entry
    for(Map.Entry<String, Integer> entry: entrySet){
        System.out.println(entry.getValue() + "\t" + entry.getKey());
    }
    }
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
字符lf='\n';
String text=“这是最好的时代,也是最糟糕的时代,”+
lf+
“这是智慧的时代,也是愚蠢的时代,”
lf+
“这是信仰的时代,也是怀疑的时代,”
lf+
“这是光明的季节,也是黑暗的季节,”
lf+
“这是希望的春天,绝望的冬天,”
lf+
“我们拥有一切,我们一无所有,”
lf+
“我们都直奔天堂,我们都直奔天堂”+
lf+
"另一方面,简而言之,这一时期到目前为止是如此"
lf+
“期间,一些最吵闹的当局坚持其”+
lf+
“为善或为恶,在最高程度上被接受”+
lf+
“仅用于比较。”+
lf+
“有一个大下巴的国王和一个相貌平平的王后,”
lf+
“在英格兰的王座上;有一位国王,他有一个大下巴和”+
lf+
“法国王座上的一位面容美丽的女王。在这两种情况下”+
lf+
“对于国家的统治者来说,这比水晶还要清晰”+
lf+
“面包和鱼的保护区,通常是”+
lf+
“永久定居”;
TreeMap map=newtreemap();
String[]words=text.split(“[\n\t\r,;:!?(){”);
for(int i=0;i0){
if(map.get(key)==null){
地图放置(图例1);
}
否则{
int value=map.get(键);
值++;
map.put(键、值);
}
}
}
Set entrySet=map.entrySet();
//从每个条目中获取键和值
for(Map.Entry:entrySet){
System.out.println(entry.getValue()+“\t”+entry.getKey());
}
}
}

另外,请您提供一个提示,告诉我如何按字母顺序排列单词。提前谢谢您

您需要对正则表达式的特殊字符进行转义。在您的情况下,您没有转义
[
{< /代码>。使用<代码> \>代码>。例如<代码> \ <代码>。您也可以考虑一个预定义的Calkter类,用于空白:<代码> \r>代码>,<代码> \t>代码>等等。

您需要避开正则表达式的特殊字符。在本例中,您没有逃过<代码>(,<代码>)<代码> > <代码> >代码> <代码> > <代码> >代码> {<代码> >。
在正则表达式的末尾

“[\n\t\r;:!?(){”
不正确

您需要将正则表达式替换为
“[\n\t\r,;:!?(){]”

您在正则表达式末尾遗漏了
“]”

“[\n\t\r;:!?(){”
不正确


您需要将正则表达式替换为“
”[\n\t\r,;:!?(){]”

您的问题是正则表达式中有一个未关闭的字符类。RegEx有一些“预定义”字符,在查找这些字符时需要转义

字符类是:

使用“字符类”,也称为“字符集”,您可以告诉正则表达式引擎只匹配几个字符中的一个。只需将要匹配的字符放在方括号中即可。

这意味着您必须转义以下字符:

\[\n\t\r\.,;:!\?\(\){
或者关闭角色类

[\n\t\r\.,;:!\?\(\){]

无论哪种方式,您都需要转义点、问号和括号。

您的问题是正则表达式中有一个未闭合的字符类。RegEx有一些“预定义”字符,在查找它们时需要转义

字符类是:

使用“字符类”,也称为“字符集”,您可以告诉正则表达式引擎只匹配几个字符中的一个。只需将要匹配的字符放在方括号中即可。

这意味着您必须转义以下字符:

\[\n\t\r\.,;:!\?\(\){
或者关闭角色类

[\n\t\r\.,;:!\?\(\){]

无论哪种方法,您都需要转义圆点、问号和圆括号。

尝试转义括号、问号和圆点。=>
\[\n\t\r\;:!\?\(\){
编译器错误还不够清楚吗?您还没有关闭正则表达式中的字符类。
“[\n\t\r,;:!?(){
应该是
”[\n\t\r;;:::!?(!?){
。非常感谢各位。我只需将
“[\n\t\r,;:!?()”{
替换为
“[\n\t\r,;:!?()”{
。。跳过括号、问号和圆点。=>
\[\n\t\r\,:!\?\(\){
编译器错误还不够清楚吗?您还没有关闭正则表达式中的字符类。
“[\n\t\r,;!?”{
应该是
“[\n\t\r;;:!?(){”
。非常感谢各位。我只需要将
“[\n\t\r;;:!?(){
替换为
”[\n\t\r;;:!?(){”
。干杯,除了字符类中的结束方括号之外,没有必要对元字符进行转义。非常感谢各位。我只需要将
“[\n\t\r;;:!?”;:!!?”!?(){“
with
”[\n\t\r;:!?(){”
。干杯除了字符类中的结束方括号外,没有必要转义元字符。非常感谢各位。我刚刚