Java replaceAll()删除所有标点符号,但有以下例外

Java replaceAll()删除所有标点符号,但有以下例外,java,regex,Java,Regex,我正在尝试使用.txt文件获取字符串[],我需要删除所有标点符号,但有一些例外。这是我的密码: replaceAll("[^a-zA-Z ]", ""); 例外情况: 1.单词内部的连字符。2.删除包含数字的单词3.删除包含结尾和开头两个标点符号的单词[^a-zA-Z]是一个字符类。这意味着它将只匹配一个字符,在本例中,它将匹配任何不是a-z、a-z或空白的字符 如果要匹配单词,需要使用带有量词的字符类,例如+。如果要匹配不同的模式,则需要应用or逻辑运算符| 知道这一点,你现在可以匹配以一个

我正在尝试使用.txt文件获取字符串[],我需要删除所有标点符号,但有一些例外。这是我的密码:

replaceAll("[^a-zA-Z ]", "");
例外情况:
1.单词内部的连字符。2.删除包含数字的单词3.删除包含结尾和开头两个标点符号的单词

[^a-zA-Z]是一个字符类。这意味着它将只匹配一个字符,在本例中,它将匹配任何不是a-z、a-z或空白的字符

如果要匹配单词,需要使用带有量词的字符类,例如+。如果要匹配不同的模式,则需要应用or逻辑运算符
|


知道这一点,你现在可以匹配以一个或多个数字结尾的单词,或者在中间有一个数字“代码> [^ a-Za-Z] [09]+[[^ a-Za-Z] ++[-9] < < /代码>)。我将把它留给你作为一个练习来应用于你的三个案例,因为这听起来像是一个学校作业。

我有非常复杂的正则表达式,但它可以工作

\S*\d+\S*|\p{Punct}{2,}\S*|\S*\p{Punct}{2,}|[\p{Punct}&&[^-]]+|(?<![a-z])\-(?![a-z])

我试着使用它,我也能部分工作,但我也会去掉单词中的连字符。它仍然是“-”和“-”+单词每个字符的功能是什么?我仍然困在那里
Match this alternative «\S*\d+\S*»
   Match a single character that is NOT a “whitespace character” «\S*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match a single character that is a “digit” «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match a single character that is NOT a “whitespace character” «\S*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Or match this alternative «\p{Punct}{2,}\S*»
   Match a character from the POSIX character class “punct” «\p{Punct}{2,}»
      Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}»
   Match a single character that is NOT a “whitespace character” «\S*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Or match this alternative «\S*\p{Punct}{2,}»
   Match a single character that is NOT a “whitespace character” «\S*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match a character from the POSIX character class “punct” «\p{Punct}{2,}»
      Between 2 and unlimited times, as many times as possible, giving back as needed (greedy) «{2,}»
Or match this alternative «[\p{Punct}&&[^-]]+»
   Match a single character present in the list below «[\p{Punct}&&[^-]]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      A character from the POSIX character class “punct” «\p{Punct}»
      Except the literal character “-” «&&[^-]»
Or match this alternative «(?<![a-z])\-(?![a-z])»
   Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<![a-z])»
      Match a single character in the range between “a” and “z” «[a-z]»
   Match the character “-” literally «\-»
   Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?![a-z])»
      Match a single character in the range between “a” and “z” «[a-z]»
String text ="a-b ab--- - ---a --- , ++++ ?%# $22 43 4zzv";

String rx = "(?i)\\S*\\d+\\S*|\\p{Punct}{2,}\\S*|\\S*\\p{Punct}{2,}|[\\p{Punct}&&[^-]]+|(?<![a-z])\\-(?![a-z])";

String result = text.replaceAll(rx, " ").trim();

System.out.println(result);
a-b