Java Regex

In this post, you will learn Java REGX and its example.

regular expression defines a pattern for a String. Regular Expressions can be used to search, edit or manipulate text. Regular expressions are not language specific but they differ slightly for each language. 

Here are some rules to be followed while creating a regular expression.







E.g.

Regular expression for validating the email id is:

^[_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


No comments:

Post a Comment