[关闭]
@thorncorona 2015-02-27T23:54:10.000000Z 字数 2385 阅读 1527

java正则表达式

正则表达式


一,java.util.regex包中提供了两个类来表示对正则表达式的支持

1.Matcher,通过解释Pattern对character sequence 执行匹配操作的引擎

public final class Matcher implements MatchResult

2.Pattern,正则表达式的编译表示形式

public final class Pattern implements java.io.Serializable

代码:

  1. package yuki.regular;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class FirstTest {
  5. public static void main(String[] args) {
  6. /**
  7. * Pattern,正则表达式的编译表示形式
  8. * public final class Pattern implements java.io.Serializable
  9. */
  10. String str = "hi! i am a tony; glad to see you!";
  11. String regex = "\\p{Punct}";
  12. Pattern pattern = Pattern.compile(regex);
  13. String[] strArr = pattern.split(str);
  14. for(int i = 0; i < strArr.length; ++i)
  15. System.out.println("strArr[" + i + "] = " + strArr[i]);
  16. /**
  17. * Matcher,通过解释Pattern对character sequence执行匹配操作
  18. * public final class Matcher implements MatchResult
  19. */
  20. Matcher matcher = pattern.matcher(str);
  21. System.out.println(matcher.matches() ? "匹配" : "不匹配");
  22. System.out.println(
  23. Pattern.compile("\\p{Punct}+").matcher(".,.;").matches()
  24. ? "匹配" : "不匹配");
  25. String s2 = "yuki@gmail.com";
  26. Pattern p2 = Pattern.compile("\\w+@\\w+.[a-zA-Z]+");
  27. Matcher m2 = p2.matcher(s2);
  28. System.out.println("m2.matches() = " + m2.matches());
  29. }
  30. }

运行结果:

  1. strArr[0] = hi
  2. strArr[1] = i am a tony
  3. strArr[2] = glad to see you
  4. 不匹配
  5. 匹配
  6. m2.matches() = true

二,String类对正则的支持

代码:

  1. package yuki.regular;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class SecondTest {
  5. public static void main(String[] args) {
  6. /**
  7. * 匹配替换
  8. */
  9. String date = "2015/2/27";
  10. Pattern p = Pattern.compile("/");
  11. Matcher m = p.matcher(date);
  12. String s = m.replaceAll("-");
  13. System.out.println("m.matches() = " + m.matches());
  14. System.out.println("s = " + s);
  15. System.out.println("m.replaceFirst(\"-\") = " + m.replaceFirst("-"));
  16. //匹配电话号码
  17. String phone = "0755-28792686";
  18. String regex = "\\d{3,4}-\\d{7,8}";
  19. boolean isPhone = phone.matches(regex);
  20. System.out.println("isPhone = " + isPhone);
  21. }
  22. }

运行结果:

  1. m.matches() = false
  2. s = 2015-2-27
  3. m.replaceFirst("-") = 2015-2/27
  4. isPhone = true

三,常用示例

代码:

  1. package yuki.regular;
  2. public class ThirdTest {
  3. public static void main(String[] args) {
  4. /**
  5. * 至少含有字符串数组中的一个
  6. */
  7. String s = "123,456,789,012,345";
  8. String s2 = "123,456,789,013,345";
  9. String regex = ".*(234|678|012).*";
  10. boolean isMatch = s.matches(regex);
  11. boolean isMatch2 = s2.matches(regex);
  12. System.out.println("isMatch = " + isMatch);
  13. System.out.println("isMatch2 = " + isMatch2);
  14. //匹配金额
  15. String price = "499.00";
  16. System.out.println("price.matches(\"\\d+.\\d+\")" + price.matches("\\d+.\\d+"));
  17. }
  18. }

运行结果:

  1. isMatch = true
  2. isMatch2 = false
  3. price.matches("\d+.\d+")true

更多参考:
API:java.util.regex
兄弟连_马剑威JAVA基础正则表达式


点击下方的红色按钮关注我吧!
孔东阳
二〇一五年二月二十七日
http://www.cnblogs.com/kodoyang/

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注