简体   繁体   English

使用Java Regex删除字符串开头的给定字符序列的出现次数

[英]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: 这是它的作用:

正则表达图像

see it live here 看到它住在这里

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()完全相同)

正则表达图像

Look it up here 在这里查找

(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.

相关问题 我应该如何计算Java中字符串开头字符出现的次数 - How should I count the number of occurrences of a character at the beginning of a string in Java 使用 REGEX 从 Java 中的 String 中删除 Unicode 字符 - To remove Unicode character from String in Java using REGEX 使用正则表达式匹配字符串的开头和结尾[Java] - Using regex to match beginning and end of string [Java] Java从字符串的开头和结尾删除所有非字母数字字符 - Java remove all non alphanumeric character from beginning and end of string 在Java中计算字符串中的字符出现次数 - count character occurrences in a string in Java "计算字符串中的字符出现次数 - Java" - Counting character occurrences in a string - Java 从用户处获取一个字符,并在 Java 中用户给出的字符串中查找该字符的出现次数 - Get a character from the user and find the no of occurrences of the character in a string given by the user in Java 在Java中使用正则表达式匹配字符串并生成序列 - Match String and generate sequence using regex in Java 使用Regex从Java中的字符串中提取序列 - Extract a sequence from a string in java using Regex 如何使用switch循环计算字符串中的多个字符出现次数? Java的 - How to count multiple character occurrences in a string using the switch loop? Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM