简体   繁体   English

正则表达式不从模式中删除下划线

[英]regex not removing underscore from pattern

I was trying to code for allowing certain special characters in a string by using java.util.regex.Matcher and java.util.regex.pattern but this is not removing underscore from the same. 我试图通过使用java.util.regex.Matcher和java.util.regex.pattern来编写允许字符串中的某些特殊字符的代码,但这不是从同一个删除下划线。 I'm new here. 我是新来的。 I need help on this. 我需要帮助。 Code extract below: 代码提取如下:

  // String to be scanned to find the pattern.
  String line = "This order was _:$ placed for QT3000! OK?";
  String pattern = "[^\\w\\s\\-?:().,'+\\/]";
  String s = null;

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);
  s= m.replaceAll("");
  System.out.println("Output: " + s);

Expected: This order was : placed for QT3000 OK? 预计:这个订单是:放置QT3000好吗? Actual : This order was _: placed for QT3000 OK? 实际:这个订单是_:QT3000的订单好吗?

The \\w pattern matches underscores and [^\\w] matches any char but letters, digits and an underscore. \\w模式匹配下划线, [^\\w]匹配任何字符,但字母,数字和下划线。

Replace with \\p{Alnum} : 替换为\\p{Alnum}

String pattern = "[^\\p{Alnum}\\s?:().,'+/-]";

Note I put the hyphen at the end of the character class so as not to escape it and remove the escaping \\ from the / as it is not a special regex metacharacter. 注意我将连字符放在字符类的末尾,以便不转义它并从/删除转义\\ ,因为它不是特殊的正则表达式元字符。

See the Java regex demo . 请参阅Java regex演示

The [^\\\\p{Alnum}\\\\s?:().,'+/-] pattern matches any char but: [^\\\\p{Alnum}\\\\s?:().,'+/-]模式匹配任何char但是:

  • \\p{Alnum} - alphanumeric [a-zA-Z0-9] \\p{Alnum} - 字母数字[a-zA-Z0-9]
  • \\s - whitespaces \\s - 空白
  • ? - a question mark - 一个问号
  • : - a colon : - 冒号
  • ( - a ( symbol ( -一(符号
  • ) - a ) symbol ) - a )符号
  • . - a dot - 一个点
  • , - a comma , - 一个逗号
  • ' - a single quotation mark ' - 单引号
  • + - a plus + - 一个加号
  • / - a forward slash / - 正斜杠
  • - - a hyphen. - - 连字符。

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

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