The character classes in Java regular expression is defined using the square brackets "[ ]", the character class matches a single character from the specified or, set of possible characters.For example, the regular expression [abc] matches a single character a or, b or, c. Similarly, "[a-z]" matches a character from a to z.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[a-z]"; ... Read More
There are 7 common non printable characters used in general and each character has its own hexadecimal representation.NamecharactersHexa-decimal representationbell\a0x07Escape\e0x1BForm feed\f0x0CLine feed0x0ACarriage return\r0X0DHorizontal tab\t0X09Vertical tab\v0X0BExample 1 Live DemoFollowing Java program accepts an input text and counts the number of tab spaces in it −import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "\t"; //Creating a pattern object Pattern pattern = ... Read More
The Possessive Quantifier [X?+] matches the X present once or not present at all.Example Live Demopackage com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PossesiveQuantifierDemo { private static final String REGEX = "T?+"; private static final String INPUT = "abcdTatW"; public static void main(String[] args) { // create a pattern Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(INPUT); while(matcher.find()) { //Prints the start index of the match. System.out.println("Match String start(): "+matcher.start()); ... Read More
You can treat multiple characters as a single unit by capturing them as groups. You just need to place these characters inside a set of parentheses.You can count the number of groups in the current match using the groupCount() method of the Matcher class. This method calculates the number of capturing groups in the current match and returns it.Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { String str1 = "This is an example HTML script where ever alternative word is bold."; //Regular expression to match contents ... Read More
Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.Whereas a reluctant or, non-greedy quantifier matches as little as possible, initially the non-greedy quantifier matches the first character if match not occurred it adds another character from the input string and tries to match.If you place a "?" after a greedy quantifier it becomes reluctant or non-greedy quantifier. Following is the list of reluctant quantifiers −QuantifierDescriptionre*?Matches zero or more occurrences.re??Matches zero or, 1 occurrence.re+?Matches one or more ... Read More
Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.A possessive quantifier is similar to a greedy quantifier the only difference is it tries to match as many character as it can initially and, if match not occurred unlike greedy quantifier it does not backtrack.If you place a "+" after a greedy quantifier it becomes possessive quantifier. Following is the list of possessive quantifiers −QuantifierDescriptionre*+Matches zero or more occurrences.re?+Matches zero or, 1 occurrence.re++Matches one or more occurrences.re{n}+Matches ... Read More
The regular expression "(?digit(?!\d)" matches the digit specified.The replaceAll() method accepts two strings: a regular expression pattern and, the replacement string and replaces the pattern with the specified string.Therefore, to remove all numbers in a string except 1 and 2, replace the regular expressions 1 and 2 with one and two respectively and replace all the other digits with an empty string.Example Live Demoimport java.util.Scanner; public class RegexExample { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String ... Read More
You can subtract one range from other and use it as new range. You can achieve this by using two variants of character classes i.e. negation and intersection.For example the intersection of ranges [a-l] and [^e-h] gives you the characters a to l as rage subtracting the characters [e-h]Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[a-l&&[^e-h]]"; //Creating a ... Read More
The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression matches a single character from the specified or, set of possible characters. For example the regular expression [abc] matches a single character a or, b or, c.The intersection variant of the character class allows you to match a character which is common in the ranges that have intersection relation between them.An intersection relation between ranges is defined using && i.e. the expression [a-z&&[r-u]] matches a single character from r to u.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 { public ... Read More
The java.util.regex.MatcheResult interface provides methods to retrieve the results of a match.You can get an object of this interface using the toMatchResult() method of the Matcher class. This method returns a MatchResult object which represents the match state of the current matcher.The group() method of this interface returns a string value representing the matched substring from the given input sequence, in the last match.Example Live Demoimport java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GroupExample { public static void main( String args[] ) { String regex = "(.*)(\d+)(.*)"; //Reading input from user ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP