简体   繁体   中英

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:

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]+", " ");
yourInputString = yourInputString.replaceAll("[^\\p{Alpha}]", " ");

^ denotes "all characters except"

\\p{Alpha} denotes all alphabetic characters

See Pattern for details.

I want to find any character that is NOT a letter

That will be [^\\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.

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. You can learn more about regexs at the great site http://regular-expression.info .

Use the regexp /[^a-zA-Z]/ which means, everything that is not in the az/AZ characters

In ruby I would do:

"contains(reserved[j]))".gsub(/[^a-zA-Z]/, " ")
 => "contains reserved j   "

In Java should be something like:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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