简体   繁体   English

用于匹配java中Pattern类中的模式的正则表达式

[英]regular expression for matching a pattern in Pattern class in java

I have a string m="hell,hj;nk,.txt" 我有一个字符串m="hell,hj;nk,.txt"

I want my string as string m="hellhjnk.txt" 我希望我的字符串为字符串m="hellhjnk.txt"

I am using: 我在用:

 Pattern p=Pattern.compile("(\"([^\"]*)(\\.)([a-z]{1,4}[\"]))|'([^']+)(\\.)([a-z]{1,4})'");

It is working for double quotes and extension. 它适用于双引号和扩展名。 How it will work for removing space,comma,semicolon? 如何删除空格,逗号,分号?

You could just do: 你可以这样做:

m = m.replaceAll("[,; ]","");

The Pattern class is used for matching. Pattern类用于匹配。 You can essentially do the same thing: 你基本上可以做同样的事情:

     Pattern p = Pattern.compile("[;, ]");
     String m = "hell,hj;nk,.txt";
     Matcher matcher = p.matcher(m);
     System.out.println(matcher.replaceAll(""));

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

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