简体   繁体   English

String.replaceAll用于多个字符

[英]String.replaceAll for multiple characters

I have a line with ^||^ as my delimiter, I am using 我有一行以^||^作为分隔符,我正在使用

int charCount = line.replaceAll("[^" + fileSeperator + "]", "").length();  
if(fileSeperator.length()>1)
{  
    charCount=charCount/fileSeperator.length();
    System.out.println(charCount+"char count between");  
}

This does not work if i have a line that has stray | 如果我的行有杂散,这将不起作用| or ^ as it counts these as well. ^因为它们也算在内。 How can i modify the regex or any other suggestions? 如何修改正则表达式或任何其他建议?

If I understand correctly, what you're really trying to do is count the number of times that ^||^ appears in your String. 如果我理解正确,那么您真正想做的就是计算^||^在您的字符串中出现的次数。

If that's the case, you can use: 如果是这样,您可以使用:

Matcher m = Pattern.compile(Pattern.quote("^||^")).matcher(line);
int count = 0;
while ( m.find() ) 
    count++;

System.out.println(count + "char count between");

But you really don't need the regex engine for this. 但是您实际上不需要正则表达式引擎。

int startIndex = 0;
int count = 0;
while ( true ) {
    int newIndex = line.indexOf(fileDelimiter, startIndex);
    if ( newIndex == -1 ) {
        break;
    } else {
        startIndex = newIndex + 1;
        count++;
    }
}

Certain characters have special meanings in a regular expression, such as ^ and | 某些字符在正则表达式中具有特殊含义,例如^| . These must be escaped with a backslash in order for them to be treated as normal characters and not as special characters. 这些字符必须以反斜杠转义,以便将它们视为普通字符而不是特殊字符。 For example, the following regular expression matches all caret ( ^ ) and pipe ( | ) characters (note the backslashes): [\\^\\|] 例如,以下正则表达式匹配所有插入符号( ^ )和竖线( | )字符(请注意反斜杠): [\\^\\|]

The Pattern.quote() method can be used to escape all of the special characters in a given String. Pattern.quote()方法可用于转义给定String中的所有特殊字符。

String quoted = Pattern.quote("^||^"); //returns "\^\|\|\^";

Also note that a character class only matches one character . 另请注意,一个字符类仅匹配一个字符 Thus, the regex [^\\^\\|\\|\\^] will match all characters except ^ and | 因此,正则表达式[^\\^\\|\\|\\^]将匹配除^|以外的所有字符| , not all characters except the sequence ^||^ . 不是序列^||^以外的所有字符。 If your intention is to count the number of delimiters ( ^||^ ) in a String, then a better approach might be to use the String.indexOf(String, int) method. 如果您打算计算字符串中定界符( ^||^ )的数量,那么更好的方法可能是使用String.indexOf(String, int)方法。

Mark Peters's answer seems better. 马克·彼得斯的答案似乎更好。 I edited so my answer won't cause any confusion. 我进行了编辑,因此我的答案不会造成任何混乱。

You should replace it like this with proper escaping since your delimiter has all special character of regex: 由于分隔符具有正则表达式的所有特殊字符,因此应使用适当的转义符将其替换为:

line.replaceAll("\\^\\|\\|\\^", "");

OR else don't use regex at all and call replace method like this: 否则根本不使用正则表达式,并调用如下的replace方法:

line.replace("^||^", "");

Lazy solutions. 懒惰的解决方案。

Depending on the end goal (the println statement is a little confusing): 根据最终目标(println语句有点混乱):

int numberOfDelimiters = (line.length() - line.replace(fileSeparator,"").length()) 
                         / fileSeparator.length();

int numberOfNonDelimiterChars = line.replace(fileSeparator,"").length();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM