Maruthi Krishna has Published 558 Articles

Finding a Match Within Another Match Java regular expressions

Maruthi Krishna

Maruthi Krishna

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

328 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

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

Matching Nonprintable Characters using Java regex

Maruthi Krishna

Maruthi Krishna

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

1K+ 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

228 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

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

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

Maruthi Krishna

Maruthi Krishna

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

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

Character class: subtraction - Java regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 12:38:17

551 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]Example Live Demoimport ... Read More

Character class: intersection - Java regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 12:35:27

356 Views

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

MatchResult end(int group) method in Java with examples.

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 12:31:43

135 Views

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 end(int group) method of this interface accepts an ... Read More

Posix character classes p{Sc} Java regex

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 12:25:21

345 Views

This class matches currency symbols.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example1 {    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

Advertisements