Java regex check non-alphanumeric string

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 alphanumeric string

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, 62 characters. We can use below regex to match alphanumeric characters: ^[a-zA-Z0-9]+$ Regex explanation ^ # start string [a-z] # lowercase letters from a to z [A-Z] # uppercase letters from A to Z [0-9] # digits from 0 to 9 + # …

Read more

Regular Expression case sensitive 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

Domain name regular expression example

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

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 …

Read more

How to extract HTML Links with regular expression

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 validate HTML tag 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

Java regex validate date format examples

This article shows how to use regex + code to validate a date format, support single and leading zero month and day format (1 or 01), check for the 30 or 31 days of the month, and leap year validation. Below are the requirements for a valid date. Year format, 1900, 2099 regex Month format, …

Read more

How to validate Time in 24 Hours format 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

Java IP Address (IPv4) regex examples

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

How to validate image file extension with regular expression

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

Java regex validate username examples

This article shows how to use regex to validate a username in Java. Username requirements Username consists of alphanumeric characters (a-zA-Z0-9), lowercase, or uppercase. Username allowed of the dot (.), underscore (_), and hyphen (-). The dot (.), underscore (_), or hyphen (-) must not be the first or last character. The dot (.), underscore …

Read more

Java regex validate password examples

This article shows how to use regex to validate a password in Java. Secure Password requirements Password must contain at least one digit [0-9]. Password must contain at least one lowercase Latin character [a-z]. Password must contain at least one uppercase Latin character [A-Z]. Password must contain at least one special character like ! @ …

Read more

Java email regex examples

The format of an email address is local-part@domain. Look at this email address [email protected] local-part = mkyong @ = @ domain = example.com The formal definitions of an email address are in RFC 5322 and RFC 3696. However, this article will not follow the above RFC for email validation. The official email "local-part" is too …

Read more