如何替换不符合条件的字符串中的所有字符.我在使用NOT运算符时遇到了麻烦.
具体来说,我试图删除所有不是数字的字符,到目前为止我已经尝试过了:
String number = "703-463-9281";
String number2 = number.replaceAll("[0-9]!", ""); // produces: "703-463-9281" (no change)
String number3 = number.replaceAll("[0-9]", ""); // produces: "--"
String number4 = number.replaceAll("![0-9]", ""); // produces: "703-463-9281" (no change)
String number6 = number.replaceAll("^[0-9]", ""); // produces: "03-463-9281"
Run Code Online (Sandbox Code Playgroud) 我有一个正则表达式来验证字符串.但现在我想删除所有与我的正则表达式不匹配的字符.
例如
regExpression = @"^([\w\'\-\+])"
text = "This is a sample text with some invalid characters -+%&()=?";
//Remove characters that do not match regExp.
result = "This is a sample text with some invalid characters -+";
Run Code Online (Sandbox Code Playgroud)
有关如何使用RegExpression确定有效字符并删除所有其他字符的任何想法.
非常感谢
我们如何编写一个正则表达式来表示“如果中间有破折号,则匹配失败”
我有几行组成为 3958.3r - 5v,有些没有破折号,例如:3958.3v4r。我能够得到带有破折号的那些,但不仅限于那些没有破折号的