The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, a total of 62 characters, and we can use regex [a-zA-Z0-9]+ to matches alphanumeric characters. If we want regex matches non-alphanumeric characters, prefix it with a negate symbol ^, meaning we want any characters that are not alphanumeric. ^[^a-zA-Z0-9]+$ Regex explanation ^ # […]

Read more Java regex check non-alphanumeric string

By default, the “.” doesn’t match line breaks. To match multiple lines, add (?s) prefix or enable the Pattern.DOTALL flag. 1. Example A address sample, we need to extract the “Address 1” only. Starting… Address 1: 88 app 2/8 superman taman, puchong 36100, Malaysia Address 2: abc End Failed : To extract “Address 1”, this […]

Read more Regular Expression matches multiple line example – Java

In Java, by default, the regular expression (regex) matching is case sensitive. To enable the regex case insensitive matching, add (?) prefix or enable the case insensitive flag directly in the Pattern.compile(). A regex pattern example. Pattern = Registrar:\\s(.*) Case Insensitive, add (?) prefix. Pattern = (?)Registrar:\\s(.*) Case Insensitive, add Pattern.CASE_INSENSITIVE flag. Pattern.compile("Registrar:\\s(.*)", Pattern.CASE_INSENSITIVE); 1. […]

Read more Regular Expression case sensitive example – Java

Domain Name Regular Expression Pattern ^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$ Above pattern makes sure domain name matches the following criteria : The domain name should be a-z | A-Z | 0-9 and hyphen(-) The domain name should between 1 and 63 characters long Last Tld must be at least two characters, and a maximum of 6 characters The domain […]

Read more Domain name regular expression example

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 […]

Read more 10 Java Regular Expression Examples You Should Know

In this tutorial, we will show you how to extract hyperlink from a HTML page. For example, to get the link from following content : this is text1 <a href=’mkyong.com’ target=’_blank’>hello</a> this is text2… First get the “value” from a tag – Result : a href=’mkyong.com’ target=’_blank’ Later get the “link” from above extracted value […]

Read more How to extract HTML Links with regular expression

HTML tag Regular Expression Pattern <("[^"]*"|'[^’]*’|[^’">])*> Description < #start with opening tag "<" ( # start of group #1 "[^"]*" # allow string with double quotes enclosed – "string" | # ..or ‘[^’]*’ # allow string with single quote enclosed – ‘string’ | # ..or [^’">] # cant contains one single quotes, double quotes and […]

Read more How to validate HTML tag with regular expression

Time in 24-Hour Format Regular Expression Pattern ([01]?[0-9]|2[0-3]):[0-5][0-9] Description ( #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] # follw by 0..5 and 0..9, which means 00 to 59 The 24-hour clock […]

Read more How to validate Time in 24 Hours format with regular expression

Time in 12-Hour Format Regular Expression Pattern (1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm) Description ( #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] # follw by 0..5 and 0..9, which means 00 to 59 (\\s)? […]

Read more How to validate Time in 12 Hours format with regular expression

This article focus on how to validate an IP address (IPv4) using regex and Apache Commons Validator. Here is the summary. IPv4 regex explanation. Java IPv4 validator, using regex. Java IPv4 validator, using commons-validator-1.7 JUnit 5 unit tests for the above IPv4 validators. IPv4 regex final version. ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.(?!$)|$)){4}$ # Explanation ( [0-9] # 0-9 | […]

Read more Java IP Address (IPv4) regex examples

Image File Extension Regular Expression Pattern ([^\s]+(\.(?i)(jpg|png|gif|bmp))$) Description ( #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 sensive checking for the following characters ( # start of the […]

Read more How to validate image file extension with regular expression