正则表达式用法
发布日期:2021-05-10 10:48:43 浏览次数:11 分类:精选文章

本文共 4009 字,大约阅读时间需要 13 分钟。

������������������Regular Expression���������regex������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

1. ������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������"abcd"������������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

2. ������������������

���������������������������������[]���������������������������������������������������&���|������������������������������������������������

  • ���������������������

    • [abc]���������a���b���c������������������������
    • [a-z]���������������������a���z������������
    • [^a-z]���������������a���z������������������������������������������������������
  • ���������������������

    • [a-z&^c]���������a���c������������������������������
    • [a-zA-Z0-9@!#]������������������������������������������@���!���#������������������

3. ������������������

������������������������������������������������������������������������������������������������������������

  • ���������������
    • .������������������������������
    • /���������������������������������[0-9]���
    • w���������������������������������[a-zA-Z0-9]���
    • s������������������������������������������������������������������������������
    • d���������������������������
    • w���������������������������
    • s���������������������������

4. ������������������

���������������������������������������������������������������������������������������������

  • ������������������������
    • *���������������������������������������������������
    • ?���������������������������������������
    • +���������������������������������������������������
    • {n}���������������n������
    • {n,}���������n���������������
    • {n,m}���������n������m������������

���������

  • a*������������������������a���
  • (agc)?���������agc���������������������������
  • /d{3}b������������d������������������������������������������������

5. ���������������������

������()���������������������������������������������������������������������������

  • ���������������������������������
    • ���������������������������������������������
    • ������������|������������������������

���������

  • (one|two)?���������������one���two���������������
  • (www|baidu).com������������www���baidu������������������

6. ���������������������

^���&���������������������������������������������������

  • ^������������������������������������
  • &������������������������������������
  • ^���&������������������������������������������������������������������

���������������

  • \���������������������������������������������������������������������������������
  • ������������������������������������������������Java���������������������

7. ������������������������������������������

1. Java������������

  • String������������

    • matches()������������������������������������������������������������
    • split()������������������������������������������������
    • replaceAll()���������������������������
    • replaceFirst()���������������������������������
  • Pattern������Matcher������

    • ������Pattern.compile������������������
    • Matcher.find()������������������������������������������������
    • Matcher.lookingAt()������������������������������������������������

���������������

Pattern pattern = Pattern.compile("\\d{3}");Matcher matcher = pattern.matcher("abc123dgh");System.out.println(matcher.matches());   // falseSystem.out.println(matcher.find());    // trueSystem.out.println(matcher.lookingAt()); // false

2. ���������������������

No other languages are covered in this document.

上一篇:java: -source 1.5 中不支持 diamond 运算符7
下一篇:正则表达式

发表评论

最新留言

不错!
[***.144.177.141]2025年04月18日 20时59分52秒