![](/img/trans.png)
[英]How should I count the number of occurrences of a character at the beginning of a string in Java
[英]Remove occurrences of a given character sequence at the beginning of a string using Java Regex
我有一个字符串,以一个或多个序列"Re:"
。 这个"Re:"
可以是任何组合,例如。 Re<any number of spaces>:
, re:
, re<any number of spaces>:
, RE:
, RE<any number of spaces>:
等。
字符串的示例序列: Re: Re : Re : re : RE: This is a Re: sample string.
我想定义一个java正则表达式,它将识别和去除所有出现的Re:
,但只有字符串开头的那些而不是字符串中出现的那些。
因此输出应该看起来像This is a Re: sample string.
这是我尝试过的:
String REGEX = "^(Re*\\p{Z}*:?|re*\\p{Z}*:?|\\p{Z}Re*\\p{Z}*:?)";
String INPUT = title;
String REPLACE = "";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT);
while(m.find()){
m.appendReplacement(sb,REPLACE);
}
m.appendTail(sb);
我正在使用p{Z}
来匹配空格(在这个论坛的某处找到了这个,因为Java正则表达不能识别\\s
)。
我在使用此代码时遇到的问题是搜索在第一次匹配时停止,并转义while循环。
尝试这样的替换语句:
yourString = yourString.replaceAll("(?i)^(\\s*re\\s*:\\s*)+", "");
正则表达式的解释:
(?i) make it case insensitive
^ anchor to start of string
( start a group (this is the "re:")
\\s* any amount of optional whitespace
re "re"
\\s* optional whitespace
: ":"
\\s* optional whitespace
) end the group (the "re:" string)
+ one or more times
在你的正则表达式:
String regex = "^(Re*\\p{Z}*:?|re*\\p{Z}*:?|\\p{Z}Re*\\p{Z}*:?)"
这是它的作用:
它匹配字符串,如:
\\p{Z}Reee\\p{Z:
或 R\\p{Z}}}
这对你尝试做的事情毫无意义:
你最好使用如下的正则表达式:
yourString.replaceAll("(?i)^(\\s*re\\s*:\\s*)+", "");
或者让@Doorknob开心,这是使用Matcher
实现这一目标的另一种方法:
Pattern p = Pattern.compile("(?i)^(\\s*re\\s*:\\s*)+");
Matcher m = p.matcher(yourString);
if (m.find())
yourString = m.replaceAll("");
( 正如doc所说的那样与yourString.replaceAll()
完全相同)
(我和@ Doorknob有相同的正则表达式,但感谢@jlordo对于replaceAll
和@Doorknob考虑(?i)
不区分大小写的部分;-))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.