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

77 comments on “10 Java Regular Expression Examples You Should Know

  1. Hi can you please provide me Rejex for password as per below :
    β€’ The minimum password length is 8 characters.
    β€’ The maximum password length is 15 characters.
    β€’ The maximum repeating character length is 2.
    β€’ The content minimum is 1 letter and one digit.
    β€’ The content is case sensitive.

  2. can can one pls. create regex for this pattern to match either for valid dates/time or for invalid date/time :
    25 January 2017 | 09:59:09 CST

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

    This is my version of the email regex revised. It allows for emails like [email protected]

    This complies with Gmail where you can have + in your email address.

  4. Thanks the explanation above is useful . Can you please help me in this .
    I want to convert

    to

    what shuld be the Reg-ex pattern for the the conversion.
    ( keeping in mind the spaces can be there in the source text .)

  5. Thanks,
    That was very helpful πŸ™‚
    I have a usecase that i’m not able to solve with regex. I want to fetch a substring between two same/different delimiters. The delimiters occur multiple times in the string. For eg: myString: mkyong_regex^2014^java_substring^extraction_123. So here how would i extract a substring which lies between nth occurence of ‘^’ and mth occurence of ‘_’. Say n = 2 and m = 3

  6. Hi,
    Thanks in advance for your help. I have to replace a string if it’s not available inside any single quote.

    e.g.
    name = ‘abc12namexyz234′ or name=’def234namewsr345name’ and name like ‘%ab123name345rt%’

    In the above I’ve to replace “name” with “ApplicantEntity.name” but I don’t want to replace the string that is available inside the single quote i.e. ‘abc12namexyz234’. The result after the replacement should be:

    ApplicantEntity.name= ‘abc12namexyz234′ or ApplicantEntity.name=’def234namewsr345name’ and ApplicantEntity.name like ‘%ab123name345rt%’

    I am trying to find a appropriate regex. But still no success on that. Please Help….

    Thanks,
    Utpal

  7. Your logic for “Email Regular Expression Pattern” is simplistic and not consistent with the RFC specifications for RFC2822- Internet Message Format

    Please also refer to RFC3696- Application Techniques for Checking and Transformation of Names.

  8. Hi Kyong,

    I am looking for a regular expression with AND condition. Currently I am using “(str1)|(str2)”, this one looks OK for OR condition.

    Example:
    Input String:
    “i thought it was a clever way to present the idea. At first, I tried to figure out how everything was connected. ”

    with below complete words match-
    present & idea = matcher should return true.
    presenting & idea = matcher should return false.

  9. The reg ex about date format, doesn’t seem to be correct, it accepts 4 digits dates straight away if you run that in Jdk 1.6 ( I am not sure about other versions of JDK). Please check.

    String myDate = “3106/12/2099”;
    String regEx = “(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)”;

    Pattern pattern = Pattern.compile(regEx);
    Matcher matcher = pattern.matcher(myDate);
    boolean result = matcher.find();
    System.out.println(“Result = “+result);

    output: Result = true (surprising)

    Regards
    Rahul A. Bhujbal.

  10. All who may have utilized these rubber based traditional ski discover they
    should test the skis in different conditions to find out once they work finest.

    It is better to leave it to the hands of the professionals since they
    know what they are doing and the fewer mistakes the better results you have and if the procedure
    is done correctly then you won’t experience any side effects like burns, skin irritations, rashes, skin infections and more. This is why I have tried all the hair density reduction approaches in the market, but to disastrous consequences.

  11. FYI you refer to the “:” character as a “semi colon” — this is incorrect and confusing. The “:” character is a “colon” and this character “;” is a “semicolon” (one word) although writing it as “semi colon” is a lot easier to read.

  12. Great Tutorial!!

    if I want to allow blank spaces in my charset for “name”, how can I do that?
    Will this work?
    String strPattern = “^[a-zA-Z](\\w*)(_*)(-*)(\\u0020*)”;

    Thanks in advance..

  13. Great examples. Thanks.

    But what is the difference between \\d (as in #6) and \d (as in #2)? Where do you use which? I was using \d in a Hive query and it didn’t work but \\d seems to work.

    Thanks again for a great refresher on regex.
    Matt

  14. My unit test accepted to create directories with REALLY funny name :

    public static final String INCORRECT_DIRECTORY  = "\n?\n:/^";

    So I decided to slightly limit the acceptable directories for the user’s best usage.

    My config goes as such now :

    <property name="incorrectDirectoryNamePattern" value=".*[\]\[$%&amp;'+,:;&lt;=&gt;\^`{|}~].*" />
    <property name="acceptableDirectoryNamePattern" value=".*([^[a-Z] -.].*/*)*" />
    

    which still allows me to create funny directories like :

    "/ !/b/"

    with a space before the exclamation mark. Technically OK but not very wise. It is also possible to create a directory with signe the @-sign …

  15. Hi there,
    I think there is a slight bug in the 24 hour time regex which will return a match even if the hour is over 24 as long as the rest is correct.
    Eg.
    26:00 matches 6:00
    87:12 matches 7:12

    The regex is working correctly but it’s returning a valid time when it shouldn’t be.

  16. Hi ….,

    I want to know Regular Expression Pattern, who check the one regular Expression Pattern,

    Ex:
    I will pass a Regular Expression Pattern as input and Regular Expression Pattern will check this input Regular Expression Pattern is correct or not.
    Plz help me.

  17. Thanks a lot for this professional presentation. Do you permit to use your regular expression? I would like to use it with modifications for different kind of file extensions and directories.

    Thanks once again!

  18. This is really a nice post which have detailed description with specific examples for specific purpose. I appreciate your efforts and want to say thanks on behalf of all guys who actually wants to learn about how to apply regular expressions in java. Thanks a lot…..
    Avinash

  19. Hello , In my app i want to use Reg-Ex for name ,like ex, Mr Amit Agraval or Mr Amit_Agraval. so how can i do that. means in any reg-ex how can i apply zero or more than one time blank space.

  20. I have been waiting so long for such a comprehensive library of regular expressions. Thank you very much for your generous post!

  21. Can any help with a regular Expression to Search a Query from Source File.
    Query can be of Form Select,Insert or Delete Statements.

  22. There’s an inconsistency in the syntax here. In #2, it shows \d as digit matching, which suggests there are regular expressions, not regular expressions in Java strings. But in #4, it uses \\. while the explanation shows that this is the regular expression construct \. and is escaped \\. only for the Java string.

  23. Hi, I have an input string like this “ABCXYZ”. So how to extract an array with “ABC” and “XYZ” values? I tried to use this regex group
    “.*?(.+?).*?” But it’s not working. Thank you for your help.

    1. It looks like HTML tags are not printed here. Let me try to put it again:
      I need to extract “ABC” and “XYZ” values in the array
      Input string:

      "<ul><li>ABC</li><li>XYZ</li></ul>"

      My regex I tried to use:

      ".*?<li>(.+?)</li>.*?"

      .

  24. I’ve seen emails containing ‘-‘, so use the following:

    ^[_A-Za-z0-9-+']+(\\.[_A-Za-z0-9-+']+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$
    
  25. I want a regular expression which checks for unwanted scripts that is XSS validation in <h;inputTextArea tag of jsf and i want to input the cv text as my input.
    Thanks for your help.

  26. Very nice post. I just stumbled upon your weblog and wanted to say that I’ve truly enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!

  27. Very handy guide – thankyou ! I had a customer requirement very close to your password one, but with the added wrinkle that the special accented german characters be excluded.

    After a lot of trial and error with unicode and posix expressions in place of A-Z etc (none of which worked), I found a negation match term was needed instead, so ended up with:

    ((?!.*[Γ€ΓΆΓΌΓ„Γ–ΓœΓŸ])(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z\Q!#$%()*+,-./:;=?@[\]^_{|}~\E]).{6,20})

    Hope this helps someone else.

  28. I am new to java script reg expression
    I need a regular expression in Java script from a multiline text box
    1. I can have only 4 lines in it.
    2. it can have 0-35 any charaters in those lines.
    3. Number lines are not manadatory means it can have 0 line but max 4 lines.

    any help will be appriciated .

    Manish

  29. Just use the official email regular expression from the W3C:

    RFC2822

    (?:[a-z0-9!#$%&’*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&’*+/=?^_`{|}~-]+)*|”(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*”)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

  30. Nice resume,
    Agree with regex for email, I did find a long time ago a “one email-validation-regular-expression to rule them all” but I lost the bookmark and never find it again πŸ™
    it was like 40 lines long (100 character per line aprox) and I remember that was created conforming the RFC where address format is described.

  31. @JoshDM, PhiLho

    Thanks for your comments and advices, ya i agree you both on email and html are too complex to match with RegEx, i seldom do so in my project as well :p. However , if we understand the RegEx well, it’s quite easy to customize it to suit your need.

    Actually this article is published as my collection and work reference, it’s good to know it is helpful to others.

  32. I always cringe when I see a regex to match e-mails. This one isn’t as broken as some others (too many just reject capital letters!) but it is OK only for the most common cases (what about IP address in the domain part?).
    Likewise, except for perfectly known generated output, using REs on HTML is a bad idea, as the rules are complex. I believe you can have a > in the value of an attribute, for example.

    That said, the article is excellent, showing concrete usage of REs, and doing an excellent job of breaking them down to help understanding them.
    Well done!

Leave a Comment

Your email address will not be published. Required fields are marked *