[英]Remove occurrences of a given character sequence at the beginning of a string using Java Regex
I have a string that begins with one or more occurrences of the sequence "Re:"
. 我有一个字符串,以一个或多个序列
"Re:"
。 This "Re:"
can be of any combinations, for ex. 这个
"Re:"
可以是任何组合,例如。 Re<any number of spaces>:
, re:
, re<any number of spaces>:
, RE:
, RE<any number of spaces>:
, etc. Re<any number of spaces>:
, re:
, re<any number of spaces>:
, RE:
, RE<any number of spaces>:
等。
Sample sequence of string : Re: Re : Re : re : RE: This is a Re: sample string.
字符串的示例序列:
Re: Re : Re : re : RE: This is a Re: sample string.
I want to define a java regular expression that will identify and strip off all occurrences of Re:
, but only the ones at the beginning of the string and not the ones occurring within the string. 我想定义一个java正则表达式,它将识别和去除所有出现的
Re:
,但只有字符串开头的那些而不是字符串中出现的那些。
So the output should look like This is a Re: sample string.
因此输出应该看起来像
This is a Re: sample string.
Here is what I have tried: 这是我尝试过的:
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);
I am using p{Z}
to match whitespaces(have found this somewhere in this forum, as Java regex does not identify \\s
). 我正在使用
p{Z}
来匹配空格(在这个论坛的某处找到了这个,因为Java正则表达不能识别\\s
)。
The problem I am facing with this code is that the search stops at the first match, and escapes the while loop. 我在使用此代码时遇到的问题是搜索在第一次匹配时停止,并转义while循环。
Try something like this replace statement: 尝试这样的替换语句:
yourString = yourString.replaceAll("(?i)^(\\s*re\\s*:\\s*)+", "");
Explanation of the regex: 正则表达式的解释:
(?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
in your regex: 在你的正则表达式:
String regex = "^(Re*\\p{Z}*:?|re*\\p{Z}*:?|\\p{Z}Re*\\p{Z}*:?)"
here is what it does: 这是它的作用:
it matches strings like: 它匹配字符串,如:
\\p{Z}Reee\\p{Z:
or \\p{Z}Reee\\p{Z:
或 R\\p{Z}}}
which make no sense for what you try to do: 这对你尝试做的事情毫无意义:
you'd better use a regex like the following: 你最好使用如下的正则表达式:
yourString.replaceAll("(?i)^(\\s*re\\s*:\\s*)+", "");
or to make @Doorknob happy, here's another way to achieve this, using a Matcher
: 或者让@Doorknob开心,这是使用
Matcher
实现这一目标的另一种方法:
Pattern p = Pattern.compile("(?i)^(\\s*re\\s*:\\s*)+");
Matcher m = p.matcher(yourString);
if (m.find())
yourString = m.replaceAll("");
(which is as the doc says the exact same thing as yourString.replaceAll()
) ( 正如doc所说的那样与
yourString.replaceAll()
完全相同)
(I had the same regex as @Doorknob, but thanks to @jlordo for the replaceAll
and @Doorknob for thinking about the (?i)
case insensitivity part ;-) ) (我和@ Doorknob有相同的正则表达式,但感谢@jlordo对于
replaceAll
和@Doorknob考虑(?i)
不区分大小写的部分;-))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.