代码之家  ›  专栏  ›  技术社区  ›  hese

删除以a开头的单词!

  •  1
  • hese  · 技术社区  · 14 年前

    在一行中,我想删除任何以 ! (否定标记)。

    有人能帮我在Java中使用正则表达式吗?

    String line = "hello world hello2 !xxx hello3, %643,!xxxxxxx. world5, !";
    String pure = line.replaceAll("?", "");
    

    我想删除所有 !xxx 在那个例子中。

    谢谢!

    3 回复  |  直到 14 年前
        1
  •  3
  •   Bart Kiers    14 年前

    我要全部删除!在那个例子中。

    尝试:

    String pure = line.replaceAll("!\\p{L}+", "");
    

    \p{L} 匹配任何unicode字母和 + 意思是“一个或多个”。

    演示:

    class Main {
      public static void main(String[] args) {
        String line = "!café hello world hello2 !xxx hello3, %643,!xxxxxxx. world5, !";
        String pure = line.replaceAll("!\\p{L}+", "");
        System.out.println(pure);
      }
    }
    

    产生:

    hello world hello2  hello3, %643,. world5, !
    
        2
  •  2
  •   zzzzBov    14 年前

    /!\w*/

    或者也许

    /!\w+/

        3
  •  0
  •   Atul Darne    10 年前

    当世界上出现分数不足时,这就不起作用了

    例:!这是一条虚线

    结果是

    _这是虚线