简体   繁体   中英

Regular Expression to replace all non alphanumeric characters except / to empty(“”) character

I want a regular expression to replace all non alphanumeric characters except '/' with ""(empty character).

This is what I did:

LibString.replaceAll(token, "[^a-zA-Z0-9]", "");

And the function implementation in LibString is:

public static String replaceAll(String source, String regex, String replacement) 
    {
        Pattern pattern = new Pattern(regex, 0);
        Replacer replacer = pattern.replacer(replacement);

        String result = replacer.replace(source);
        return result;
    }  

The String I gave is :

'France vs Tonga'. French International Rugby Blitz H/L 2013. Highlights of the French international Rugby match.

Expected result:

A list of words:

France
vs
Tonga
French
International
H/L
.. etc

Actual Result is:

'France
[empty]
Tonga'.
.French
[empty]

Please advice.

PLEASE NOTE: I am working on a system which has JVM 1.2 so cant use any of the util.regex or String Replace all kind of methods.
I have imported jregex.jar to my project to utilize the usage of regular expressions.

使用正确的[^a-zA-Z0-9//] insted [^a-zA-Z0-9]逃脱/

 LibString.replaceAll(token, "[^a-zA-Z0-9//]", "");

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