Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 除了所有格名词之外,从字符串中删除单引号的正则表达式?_Java_Regex_String_Replace_String Parsing - Fatal编程技术网

Java 除了所有格名词之外,从字符串中删除单引号的正则表达式?

Java 除了所有格名词之外,从字符串中删除单引号的正则表达式?,java,regex,string,replace,string-parsing,Java,Regex,String,Replace,String Parsing,我有以下Java正则表达式: String regex = "[^\\s\\p{L}\\p{N}]"; Pattern p = Pattern.compile(regex); String phrase = "Time flies: "when you're having fun!" Can't wait, 'until' next summer :)"; String delimited = p.matcher(phrase).replaceAll(""); 现在这个正则表达式删除了所有非

我有以下Java正则表达式:

String regex = "[^\\s\\p{L}\\p{N}]";
Pattern p = Pattern.compile(regex);

String phrase = "Time flies: "when you're having fun!" Can't wait, 'until' next summer :)";
String delimited = p.matcher(phrase).replaceAll("");
现在这个正则表达式删除了所有非空格和非对数

Input: Time flies: "when you're having fun!" Can't wait, 'until' next summer :)
Output: Time flies when youre having fun Cant wait until next summer
问题是,我想保留单词上的单引号,如you's、can't等,但想删除句子末尾的单引号,或是围绕单词的单引号,如“hello”。 这就是我想要的:

Input: Time flies: "when you're having fun!" Can't wait, 'until' next summer :)
Output: Time flies when you're having fun Can't wait until next summer
我如何更新我当前的正则表达式才能做到这一点?我需要保留\p{L}和\p{N},因为它必须适用于多种语言


谢谢

这应该是您想要的,或者接近:

String regex = "[^\\s\\p{L}\\p{N}']|(?<=(^|\\s))'|'(?=($|\\s))";

String regex=“[^\\s\\p{L}\\p{N}]”(?
\\p{L}\\p{N}
可能是问题的一部分;单独指示
'
有什么错?因为这样会完全删除单引号。我想保留它们作为“your's”或“johnson's”之类的所有格,但不完全是这样做的模式,
[\\s][\'“]|[\”][\\s]|[\”]$
可能会起作用(可能需要一些调整,因为我没有测试就这样做了)。然后用
\s
…替换它,因为它值得“不能”和“你是”不是所有格,它们是缩略语。它们的共同点是一个单引号,两边都有字符。所有格在单引号后可能有字符,也可能没有字符。只需在引号前后使用单词边界。先生,你是一头野兽。做得很好!被选为最佳。谢谢!复数所有格呢?正如在“复数所有格对这个正则表达式的影响将是出乎意料的”一文中,我想我已经谈到了这一点(这与
James'
的情况相同)。