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

Java 8 stream and regular expression examples. Note Learn the basic regular expression at Wikipedia 1. String.matches(regex) 1.1 This example, check if the string is a number. JavaRegEx1.java package com.mkyong.regex; import java.util.Arrays; import java.util.List; public class JavaRegEx1 { public static void main(String[] args) { List<String> numbers = Arrays.asList("1", "20", "A1", "333", "A2A211"); for (String number […]

Read more Java Regular Expression Examples

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

In Java, we can use ProcessBuilder or Runtime.getRuntime().exec to execute external shell command : 1. ProcessBuilder ProcessBuilder processBuilder = new ProcessBuilder(); // — Linux — // Run a shell command processBuilder.command("bash", "-c", "ls /home/mkyong/"); // Run a shell script //processBuilder.command("path/to/hello.sh"); // — Windows — // Run a command //processBuilder.command("cmd.exe", "/c", "dir C:\\Users\\mkyong"); // Run a […]

Read more How to execute shell command from Java

To validate email, uses JSF <f:validateRegex>, and puts following regular expression. This regex should be able to validates most of the email format, and I’m using it for few projects. Email Regular Expression ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$ P.S For detail explanation, refer to this how to validate email address with regular expression. In this tutorial, we will show […]

Read more PrimeFaces + JSF Email validator example

Spring EL supports regular expression using a simple keyword “matches“, which is really awesome! For examples, @Value("#{‘100’ matches ‘\\d+’ }") private boolean isDigit; It test whether ‘100‘ is a valid digit via regular expression ‘\\d+‘. Spring EL in Annotation See following Spring EL regular expression examples, some mixed with ternary operator, which makes Spring EL […]

Read more Spring EL regular expression example

“f:validateRegex” is a new validator tag in JSF 2.0, which is used to validate JSF component with a given regular expression pattern. For example, <h:inputSecret id="password" value="#{user.password}"> <f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" /> </h:inputSecret> The above regex pattern is required 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter […]

Read more JSF 2 validateRegex 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