Java正则表达式,用于删除除“以外的所有单个字母;a「;及;我";从字符串
我有一个文本字符串,比如Java正则表达式,用于删除除“以外的所有单个字母;a「;及;我";从字符串,java,regex,Java,Regex,我有一个文本字符串,比如 "there i r u w want to y z go because f g of a matter" 我想删除除“a”和“I”之外的所有单个字母。 所以上面给出的示例字符串如下 “因为一件事我想去那里” 除了“a”和“i”之外,删除所有这些单个字母的java正则表达式是什么?代码 用法 如果需要不区分大小写的匹配,请将Pattern.case\u insensitive传递到Patter.compile 结果 输入 输出 解释 (?:^ |)在行首断
"there i r u w want to y z go because f g of a matter"
我想删除除“a”和“I”之外的所有单个字母。
所以上面给出的示例字符串如下
“因为一件事我想去那里
”
除了“a”和“i”之外,删除所有这些单个字母的java正则表达式是什么?代码
用法
如果需要不区分大小写的匹配,请将Pattern.case\u insensitive
传递到Patter.compile
结果 输入 输出
解释
在行首断言位置或按字面匹配空格(?:^ |)
匹配除[b-hj-z]
或a
i
正向前瞻,确保后面是空格或行尾(?=|$)
这个
\b[b-hj-z]
或者“[b-hj-z](?=)”?我不知道,我想用regex替换我的名字。我被否决了,因为“给我代码”/“为我做我的工作”。哇,是的,很有效,谢谢你:)@zoric99你可以接受它作为答案:)
(?:^| )[b-hj-z](?= |$)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
final String regex = "(?:^| )[b-hj-z](?= |$)";
final String string = "there i r u w want to y z go because f g of a matter";
final String subst = "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
there i r u w want to y z go because f g of a matter
there i want to go because of a matter
String resultStr = "there i r u w want to y z go because f g of a matter".replaceAll("(?:^| )[b-hj-z | B-HJ-Z](?= |$)", "");