Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式从java字符串中删除除-和uu之外的所有标点符号_Java_Regex_String_Character Class - Fatal编程技术网

使用正则表达式从java字符串中删除除-和uu之外的所有标点符号

使用正则表达式从java字符串中删除除-和uu之外的所有标点符号,java,regex,string,character-class,Java,Regex,String,Character Class,我试图用我在这里找到的方法替换除-和u之外的所有标点符号,但我只能让它“使用发布的确切代码,它使用了负面前瞻: (?!")\\p{punct} //Java example: String string = ".\"'"; System.out.println(string.replaceAll("(?!\")\\p{Punct}", "")); 我试过: name = name.replaceAll("(?!_-)\\p{Punct}", ""); // which just repla

我试图用我在这里找到的方法替换除-和u之外的所有标点符号,但我只能让它“使用发布的确切代码,它使用了负面前瞻:

(?!")\\p{punct}

//Java example:

String string = ".\"'";
System.out.println(string.replaceAll("(?!\")\\p{Punct}", ""));
我试过:

name = name.replaceAll("(?!_-)\\p{Punct}", ""); // which just replaces all punctuation.

name = name.replaceAll("(?!\_-)\\p{Punct}", ""); // which gives an error.
谢谢。

使用一个(并添加一个
+
量词来匹配一个或多个标点字符块):

[\\p{Punct}&[^{u-]+
表示匹配
\p{Punct}
类中的任何字符,但
-
除外


您找到的结构也可以使用,但您需要将
-
-
放入字符类,并使用
.replaceAll((?![-])\\p{Punct},“”)
.replaceAll((?:(?![[u-])\\ p{Punct}+,“”)

很好。@Wiktor任何学习reg exp的好教程,请?可以解释一些高级教程吗?请先阅读。然后,我可以建议在上完成所有课程,通读,(以及许多其他指向优秀在线资源的链接),以及所谓的社区帖子。也值得一看。谢谢@WiktorStribiżew@seanEd请考虑接受答案。如果你登录,请考虑接受答案,如果它对你有用。
name = name.replaceAll("[\\p{Punct}&&[^_-]]+", "");