`

10 Java Regular Expression Examples You Should Know

    博客分类:
  • Java
 
阅读更多
转自
http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/


Regular expression is an art of the programing, it’s hard to debug , learn and understand, but the powerful features are still attract many developers to code regular expression. Let’s explore the following 10 practical regular expression ~ enjoy
1. Username Regular Expression Pattern

^[a-z0-9_-]{3,15}$

^                    # Start of the line
  [a-z0-9_-]      # Match characters and symbols in the list, a-z, 0-9 , underscore , hyphen
             {3,15}  # Length at least 3 characters and maximum length of 15
$                    # End of the line

==> See the explanation and example here
2. Password Regular Expression Pattern

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})

( # Start of group
  (?=.*\d) #   must contains one digit from 0-9
  (?=.*[a-z]) #   must contains one lowercase characters
  (?=.*[A-Z]) #   must contains one uppercase characters
  (?=.*[@#$%]) #   must contains one special symbols in the list "@#$%"
              . #     match anything with previous condition checking
                {6,20} #        length at least 6 characters and maximum of 20
) # End of group

==> See the explanation and example here
3. Hexadecimal Color Code Regular Expression Pattern

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

^ #start of the line
# #  must constains a "#" symbols
( #  start of group #1
  [A-Fa-f0-9]{6} #    any strings in the list, with length of 6
  | #    ..or
  [A-Fa-f0-9]{3} #    any strings in the list, with length of 3
) #  end of group #1
$ #end of the line

==> See the explanation and example here
4. Email Regular Expression Pattern

^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+
(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

^ #start of the line
  [_A-Za-z0-9-]+ #  must start with string in the bracket [ ], must contains one or more (+)
  ( #  start of group #1
    \\.[_A-Za-z0-9-]+ #     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
  )* #  end of group #1, this group is optional (*)
    @ #     must contains a "@" symbol
     [A-Za-z0-9]+       #        follow by string in the bracket [ ], must contains one or more (+)
      ( #    start of group #2 - first level TLD checking
       \\.[A-Za-z0-9]+  #      follow by a dot "." and string in the bracket [ ], must contains one or more (+)
      )* #    end of group #2, this group is optional (*)
      ( #    start of group #3 - second level TLD checking
       \\.[A-Za-z]{2,}  #      follow by a dot "." and string in the bracket [ ], with minimum length of 2
      ) #    end of group #3
$ #end of the line

==> See the explanation and example here
5. Image File Extension Regular Expression Pattern

([^\s]+(\.(?i)(jpg|png|gif|bmp))$)

( #Start of the group #1
[^\s]+ #  must contains one or more anything (except white space)
       ( #    start of the group #2
         \. # follow by a dot "."
         (?i) # ignore the case sensitive checking
             ( #   start of the group #3
              jpg #     contains characters "jpg"
              | #     ..or
              png #     contains characters "png"
              | #     ..or
              gif #     contains characters "gif"
              | #     ..or
              bmp #     contains characters "bmp"
             ) #   end of the group #3
       ) #     end of the group #2
  $ #  end of the string
) #end of the group #1

==> See the explanation and example here
6. IP Address Regular Expression Pattern

^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.
([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$

^ #start of the line
( #  start of group #1
   [01]?\\d\\d? #    Can be one or two digits. If three digits appear, it must start either 0 or 1
#    e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9])
    | #    ...or
   2[0-4]\\d #    start with 2, follow by 0-4 and end with any digit (2[0-4][0-9])
    |           #    ...or
   25[0-5]      #    start with 2, follow by 5 and end with 0-5 (25[0-5])
) #  end of group #2
  \.            #  follow by a dot "."
....            # repeat with 3 time (3x)
$ #end of the line

==> See the explanation and example here
7. Time Format Regular Expression Pattern
Time in 12-Hour Format Regular Expression Pattern

(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)

( #start of group #1
1[012] #  start with 10, 11, 12
| #  or
[1-9] #  start with 1,2,...9
) #end of group #1
: #    follow by a semi colon (:)
  [0-5][0-9] #   follow by 0..5 and 0..9, which means 00 to 59
            (\\s)? #        follow by a white space (optional)
                  (?i) #          next checking is case insensitive
                      (am|pm) #            follow by am or pm

==> See the explanation and example here
Time in 24-Hour Format Regular Expression Pattern

([01]?[0-9]|2[0-3]):[0-5][0-9]

( #start of group #1
[01]?[0-9] #  start with 0-9,1-9,00-09,10-19
| #  or
2[0-3] #  start with 20-23
) #end of group #1
: #  follow by a semi colon (:)
  [0-5][0-9] #    follow by 0..5 and 0..9, which means 00 to 59

==> See the explanation and example here
8. Date Format (dd/mm/yyyy) Regular Expression Pattern

(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)

( #start of group #1
0?[1-9] #  01-09 or 1-9
|                  #  ..or
[12][0-9] #  10-19 or 20-29
| #  ..or
3[01] #  30, 31
) #end of group #1
  / #  follow by a "/"
   ( #    start of group #2
    0?[1-9] # 01-09 or 1-9
    | # ..or
    1[012] # 10,11,12
    ) #    end of group #2
     / # follow by a "/"
      ( #   start of group #3
       (19|20)\\d\\d #     19[0-9][0-9] or 20[0-9][0-9]
       ) #   end of group #3

==> See the explanation and example here
9. HTML tag Regular Expression Pattern

<("[^"]*"|'[^']*'|[^'">])*>

<   #start with opening tag "<"
( #   start of group #1
   "[^"]*" # only two double quotes are allow - "string"
   | # ..or
   '[^']*' # only two single quotes are allow - 'string'
   | # ..or
   [^'">] # cant contains one single quotes, double quotes and ">"
) #   end of group #1
* # 0 or more
> #end with closing tag ">"

==> See the explanation and example here
10. HTML links Regular Expression Pattern
HTML A tag Regular Expression Pattern

(?i)<a([^>]+)>(.+?)</a>

( #start of group #1
?i #  all checking are case insensive
) #end of group #1
<a              #start with "<a"
  ( #  start of group #2
    [^>]+ #     anything except (">"), at least one character
   ) #  end of group #2
  > #     follow by ">"
    (.+?) # match anything
         </a> #   end with "</a>

Extract HTML link Regular Expression Pattern

\s*(?i)href\s*=\s*(\"([^"]*\")|'[^']*'|([^'">\s]+));

\s*    #can start with whitespace
  (?i)    # all checking are case insensive
     href    #  follow by "href" word
        \s*=\s*    #   allows spaces on either side of the equal sign,
              (    #    start of group #1
               "([^"]*")   #      only two double quotes are allow - "string"
               |    #   ..or
               '[^']*'    #      only two single quotes are allow - 'string'
               |           #   ..or
               ([^'">]+)   #     cant contains one single / double quotes and ">"
      )    #    end of group #1

==> See the explanation and example here
分享到:
评论

相关推荐

    OnJava8-Examples-3.0_soucecode_java_

    《OnJava8-Examples-3.0_soucecode_java_》是基于Java 8的一份源代码库,它对应于《Thinking in Java 5th Edition》这本书中的示例代码。这个压缩包包含了丰富的编程示例,旨在帮助读者深入理解Java 8的新特性以及...

    javacv-examples-0.7-src.zip )

    这个“javacv-examples-0.7-src.zip”压缩包包含了用于学习如何在Java环境下使用JavaCV进行计算机视觉编程的源代码示例。这些示例对于初学者来说是非常宝贵的资源,可以帮助他们快速理解和应用JavaCV库。 OpenCV是...

    java_100examples.rar_Java 小程序_Java_100examples.rar

    这个名为"java_100examples.rar"的压缩包文件显然包含了100个Java编程示例,旨在帮助初学者理解并掌握Java编程的基础概念和技巧。下面我们将详细探讨这些Java小程序可能涵盖的知识点。 首先,基础语法是所有编程...

    Java_100examples.rar_Java 100例_Java_100examples.rar_java 100例

    "Java_100examples.rar" 是一个专门为了帮助学习者深入理解和实践Java编程而编译的资源包,其中包含了100个精心挑选的Java实例,涵盖了从基础语法到高级特性的各种应用。 首先,让我们来看看这个压缩包中的...

    Java Cryptography with Examples

    本资源"Java Cryptography with Examples"提供了关于如何在Java环境中应用这些概念的详细教程。 首先,Java通过Java Cryptography Architecture (JCA) 和 Java Cryptography Extension (JCE) 提供了全面的加密支持...

    OnJava8-Examples-master

    《OnJava8-Examples-master》是一个关于Java 8编程实践的资源库,它包含了大量示例代码,旨在帮助开发者深入理解和应用Java 8的新特性。这个压缩包中的内容主要是为了展示如何在实际项目中有效利用Java 8的功能,...

    JAVA NUT 5 examples

    **JAVA NUT 5 Examples 知识点详解** JAVA NUT 5 Examples 是一组与Java编程相关的示例代码,主要用于教学和实践。这个资源可能是Javanut系列教程的一部分,特别是第5版的内容。尽管描述中提到可能不完整,但它依然...

    第三版 Java Examples In A Nutshell

    《第三版 Java Examples In A Nutshell》是一本深入浅出的Java编程教程,被誉为Java学习者的进阶之作。这本书由O'Reilly出版社出版,以其独特的"Nutshell"系列风格,为读者提供了丰富的代码示例和详尽的解释,旨在...

    java examples

    这份名为"java examples"的压缩包文件提供了丰富的Java编程示例,旨在帮助学习者深入理解和掌握Java标准库中的类以及它们的方法用法。这些例子涵盖了基础语法、数据类型、控制结构、类与对象、异常处理、集合框架、...

    javacv-examples-20121112-src

    这个“javacv-examples-20121112-src”压缩包包含了2012年11月12日版本的JavaCV示例源代码,是学习和理解JavaCV功能和用法的重要资源。 JavaCV的核心目标是简化Java程序员在处理图像处理、视频分析和计算机视觉任务...

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    It begins with background on floating-point representation and rounding error, continues with a discussion of the IEEE floating-point standard, and concludes with numerous examples of how computer ...

    java网络编程examples.zip

    "java网络编程examples.zip"这个压缩包很可能包含了一系列示例代码,帮助开发者理解和实践Java在网络编程中的应用。下面,我们将深入探讨Java网络编程的核心概念、常用API以及如何通过例子来学习。 1. Java网络编程...

    Java_examples.rar_java examples_progressbardemo.java

    "Java_examples.rar" 是一个包含100多个Java程序实例的压缩包,旨在帮助初学者深入理解和掌握Java编程技术。这个资源包特别关注了“progressbardemo.java”,这是一个关于进度条控件的示例,对于那些想学习如何在...

    Java3D-Examples.rar_java3d_netbeans java3d

    在本压缩包"Java3D-Examples.rar"中,包含了使用NetBeans构建的Java3D源代码示例。NetBeans是一个流行的开源集成开发环境(IDE),支持多种编程语言,包括Java,为开发者提供了便捷的代码编辑、调试和项目管理功能。...

    Java_examples.zip_java examples

    这个"Java_examples.zip"压缩包包含的是专为初学者设计的100个Java示例程序,旨在帮助新接触Java编程的人快速掌握基础概念和实践技巧。 1. **面向对象编程**:Java的核心特性之一是它的面向对象编程(OOP)模型,...

    Java_Web_Examples-master

    【Java_Web_Examples-master】是一个关于Java Web开发的资源集合,主要包含了各种示例代码,帮助开发者理解和学习如何在实际项目中应用Java Web技术。这个压缩包可能包含了一个具体的项目——“超市库存管理系统”,...

    Java_Examples_in_a_Nutshell_3rd

    《Java_Examples_in_a_Nutshell_3rd》是一本深受Java开发者喜爱的实战型书籍,第三版在原有的基础上进一步完善了对Java编程语言的深入解释。这本书以其丰富的示例和详细注解,帮助读者从实际操作的角度理解并掌握...

    Java Examples in a Nutshell(3rd) epub

    Java Examples in a Nutshell(3rd) 英文epub 第3版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

Global site tag (gtag.js) - Google Analytics