Maruthi Krishna has Published 951 Articles

Replacing all the matched contents Java regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 13-Jan-2020 06:34:25

1K+ 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 ... Read More

Finding a Match Within Another Match Java regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 13-Jan-2020 06:30:59

212 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.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherExample {    public static void ... Read More

Getting the list of all the matches Java regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 13-Jan-2020 06:27:52

8K+ 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.Example Live Demoimport 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) {     ... Read More

Determining the position and length of the match Java regex

Maruthi Krishna

Maruthi Krishna

Updated on 13-Jan-2020 06:22:03

895 Views

The start() method of the java.util.regex.Matcher class returns the starting position of the match (if a match occurred).Similarly, the end() method of the Matcher class returns the ending position of the match.Therefore, return value of the start() method will be the starting position of the match and the difference between ... Read More

Matching a whole word Java Regular expressions:

Maruthi Krishna

Maruthi Krishna

Updated on 13-Jan-2020 06:12:00

2K+ Views

The meta character "\b" matches word boundaries. i.e. it matches before the first and after the last word characters and between word and non-word characters.Therefore to match a whole word you need to surround it between the word boundary meta characters as −\btest\bExample Live DemoFollowing Java example counts and prints the ... Read More

Matching from a set of characters Java regualr expression

Maruthi Krishna

Maruthi Krishna

Updated on 13-Jan-2020 06:09:00

84 Views

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 ... Read More

Matching Nonprintable Characters using Java regex

Maruthi Krishna

Maruthi Krishna

Updated on 13-Jan-2020 06:06:18

829 Views

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 ... Read More

Regular expression “[X?+] ” Metacharacter Java

Maruthi Krishna

Maruthi Krishna

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

138 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) {       ... Read More

Counting the number of groups Java regular expression

Maruthi Krishna

Maruthi Krishna

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

901 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 ... Read More

Reluctant quantifiers Java Regular expressions

Maruthi Krishna

Maruthi Krishna

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

744 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 ... Read More

Advertisements