[英]Can you help with regular expressions in Java?
I have a bunch of strings which may of may not have random symbols and numbers in them.我有一堆字符串,其中可能没有随机符号和数字。 Some examples are:
一些例子是:
contains(reserved[j])){
close();
i++){
letters[20]=word
I want to find any character that is NOT a letter, and replace it with a white space, so the above examples look like:我想找到任何不是字母的字符,并将其替换为空格,因此上面的示例如下所示:
contains reserved j
close
i
letters word
What is the best way to do this?做这个的最好方式是什么?
It depends what you mean by "not a letter", but assuming you mean that letters are az or AZ then try this:这取决于你所说的“不是一个字母”是什么意思,但假设你的意思是字母是 az 或 AZ 然后试试这个:
s = s.replaceAll("[^a-zA-Z]", " ");
If you want to collapse multiple symbols into a single space then add a plus at the end of the regular expression.如果要将多个符号折叠到一个空格中,请在正则表达式的末尾添加一个加号。
s = s.replaceAll("[^a-zA-Z]+", " ");
I want to find any character that is NOT a letter
我想找到任何不是字母的字符
That will be [^\\p{Alpha}]+
.那将是
[^\\p{Alpha}]+
。 The []
indicate a group. []
表示一个组。 The \\p{Alpha}
matches any alphabetic character (both uppercase and lowercase, it does basically the same as \\p{Upper}\\p{Lower}
and a-zA-Z
. The ^
inside group inverses the matches. The +
indicates one-or-many matches in sequence. \\p{Alpha}
匹配任何字母字符(大写和小写,它与\\p{Upper}\\p{Lower}
和a-zA-Z
基本相同。组内的^
反转匹配。 +
表示按顺序进行一对多匹配。
and replace it with a white space
并用空白替换它
That will be " "
.那将是
" "
。
Summarized:总结:
string = string.replaceAll("[^\\p{Alpha}]+", " ");
Also see the java.util.regex.Pattern
javadoc for a concise overview of available patterns.另请参阅
java.util.regex.Pattern
javadoc以获得可用模式的简要概述。 You can learn more about regexs at the great site http://regular-expression.info .您可以在伟大的站点http://regular-expression.info 上了解有关正则表达式的更多信息。
Use the regexp /[^a-zA-Z]/ which means, everything that is not in the az/AZ characters使用正则表达式 /[^a-zA-Z]/ 这意味着不在z/AZ 字符中的所有内容
In ruby I would do:在 ruby 中,我会这样做:
"contains(reserved[j]))".gsub(/[^a-zA-Z]/, " ")
=> "contains reserved j "
In Java should be something like:在 Java 中应该是这样的:
import java.util.regex.*;
...
String inputStr = "contains(reserved[j])){";
String patternStr = "[^a-zA-Z]";
String replacementStr = " ";
// Compile regular expression
Pattern pattern = Pattern.compile(patternStr);
// Replace all occurrences of pattern in input
Matcher matcher = pattern.matcher(inputStr);
String output = matcher.replaceAll(replacementStr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.