Found 7442 Articles for Java

Regular expression “[X?+] ” Metacharacter Java

Maruthi Krishna
Updated on 13-Jan-2020 06:02:27

196 Views

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

Counting the number of groups Java regular expression

Maruthi Krishna
Updated on 13-Jan-2020 05:59:52

1K+ Views

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

Explain character classes in Java regular expressions

Deepti S
Updated on 29-Aug-2023 15:47:34

420 Views

Java regular expressions, is often known as rege. It is employed for searching and manipulating text. They are utilized in numerous applications. Online scraping, email screening, and password validation are a few of them.A pattern that can be employed to match a specific character sequence in a string is also known as a regular expression. To build the pattern, a special syntax involving quantifiers, character classes, wildcard characters, and ordinary characters is employed. Character classes are characters that can be matched by a regular expression. They are defined using square brackets []. For instance, the character class [abc] matches the ... Read More

Reluctant quantifiers Java Regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 05:53:17

956 Views

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

Java regex program to match parenthesis "(" or, ")".

Maruthi Krishna
Updated on 21-Feb-2020 11:27:01

6K+ Views

Following regular expression accepts a string with parenthesis −"^.*[\(\)].*$";^ matches the starting of the sentence..* Matches zero or more (any) characters.[\(\)] matching parenthesis.$ indicates the end of the sentence.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest {    public static void main( String args[] ) {       String regex = "^.*[\(\)].*$";       //Reading input from user       Scanner sc = new Scanner(System.in);       System.out.println("Enter data: ");       String input = sc.nextLine();       //Instantiating the Pattern class       Pattern pattern = Pattern.compile(regex);     ... Read More

Explain quantifiers in Java regular expressions

Alshifa Hasnain
Updated on 12-Jun-2025 17:31:35

650 Views

Quantifiers in Java are special characters that allow you to specify the number of times a character or group of characters can occur in a regular expression. The most common quantifiers are: *: One or more instances of the character or set of characters that came before it. ?: The character or set of characters before it, either zero or one instance. ... Read More

Posix character classes p{Blank} Java regex

Maruthi Krishna
Updated on 21-Feb-2020 11:11:04

302 Views

This class matches all tabs or spaces.Example  Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PrintableCharacters {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{Blank}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;   ... Read More

Possessive quantifiers Java Regular expressions

Maruthi Krishna
Updated on 10-Jan-2020 12:46:14

427 Views

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

Java program to remove all numbers in a string except "1" and "2"?

Maruthi Krishna
Updated on 10-Jan-2020 12:44:08

236 Views

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

Regular Expression Q Metacharacter in Java

Maruthi Krishna
Updated on 21-Feb-2020 11:21:57

1K+ Views

The subexpression/metacharacter "\Q" escapes all characters up to "\E" i.e. you can escape metacharacters in the regular expressions by placing them in between \Q and \E. For example, the expression [aeiou] matches the strings with vowel letters in it.Example  Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleProgram {    public static void main( String args[] ) {       String regex = "[aeiou]";       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string: ");       String input = sc.nextLine();       //Creating a Pattern object       Pattern pattern = ... Read More

Advertisements