Found 2616 Articles for Java

Possessive quantifiers Java Regular expressions

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

295 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

185 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

985 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

Character class: subtraction - Java regular expressions

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

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

Character class: intersection - Java regular expressions

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

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

MatchResult group() method in Java with examples.

Maruthi Krishna
Updated on 10-Jan-2020 12:33:40

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

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

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

49 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 integer representing a particular group and returns the offset after the last match occurred in the specified group.Example Live Demoimport java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       String regex = "(.*)(\d+)(.*)";       //Reading ... Read More

MatchResult end() method in Java with examples.

Maruthi Krishna
Updated on 10-Jan-2020 12:27:29

56 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() method of this interface returns the offset after the last match occurred.Example Live Demoimport java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       String regex = "you$";       //Reading input from user       Scanner sc = new Scanner(System.in);     ... Read More

Posix character classes p{Sc} Java regex

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

202 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 = sc.nextLine();       //Regular expression       String regex = "\p{Sc}";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;     ... Read More

Posix character classes Java regex

Maruthi Krishna
Updated on 10-Jan-2020 12:17:55

315 Views

This class \p{IsAlphabetic} matches alphabetic characters.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 = sc.nextLine();       //Regular expression       String regex = "\p{IsAlphabetic}";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;   ... Read More

Advertisements