Java Articles

Page 24 of 450

Character class: subtraction - Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 561 Views

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]Exampleimport 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 pattern ...

Read More

Regular Expression Q Metacharacter in Java

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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 import 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 = Pattern.compile(regex); ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 280 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.Exampleimport 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 input ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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 1import 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

Counting the number of groups Java regular expression

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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.Exampleimport 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 of ...

Read More

Regular expression “[X?+] ” Metacharacter Java

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 231 Views

The Possessive Quantifier [X?+] matches the X present once or not present at all.Examplepackage 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

Non capturing groups Java regular expressions:

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

Using capturing groups you can treat multiple characters as a single unit. You just need to place the characters to be grouped inside a set of parentheses. For example −(.*)(\d+)(.*)If you are trying to match multiple groups the match results of each group is captured. You can get the results a group by passing its respective group number to the group() method. 1, 2, 3 etc.. (from right to left) group 0 indicates the whole match.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CapturingGroups {    public static void main( String args[] ) {       System.out.println("Enter input text"); ...

Read More

Getting the list of all the matches Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 11K+ Views

Java does not provide any method to retrieve the list of all matches we need to use Lists and add the results to it in the while loop.Exampleimport java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ListOfMatches{    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "\d+";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       ArrayList list = new ArrayList();     ...

Read More

Finding a Match Within Another Match Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 333 Views

To match a pattern within another match you need to compile the regular expression to match the outer pattern find the match retrieve the results and pass the results as input to the inner Matcher object.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherExample {    public static void main(String[] args) {       int start = 0, len = -1;       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regexOuter = "(.*?)";       String regexInner = "\d+";     ...

Read More

Replacing all the matched contents Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

Once you compile the required regular expression and retrieved the matcher object by passing the input string as a parameter to the matcher() method.You can replace all the matched parts of the input string with another str4ing using the replaceAll() method of the Matcher class.This method accepts a string (replacement string) and replaces all the matches in the input string with it and returns the result.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAll{    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       ...

Read More
Showing 231–240 of 4,498 articles
« Prev 1 22 23 24 25 26 450 Next »
Advertisements