Found 2616 Articles for Java

Types of quantifiers in Java regex

Maruthi Krishna
Updated on 13-Jul-2020 12:14:40

622 Views

If you want to specify the number of occurrences while constructing a regular expression you can use quantifiers. Java supports three types of quantifiers namely: greedy quantifiers, reluctant quantifiers and possessive quantifiers.Greedy quantifiers − 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.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");     ... Read More

Posix character classes p{Print} Java regex

Maruthi Krishna
Updated on 09-Jan-2020 10:22:45

836 Views

This class matches all the printable characters.Example 1 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{Print}]";       //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 p{Graph} Java regex

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

394 Views

This class matches all visible characters i.e. alphabets, digits, punctuation marks.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class VisiblieCharacters {    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{Graph}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       ... Read More

Posix character classes p{Punct} Java regex

Maruthi Krishna
Updated on 09-Jan-2020 09:59:48

3K+ Views

This class matches punctuation marks. i.e.!"#$%&'()*+, -./:;?@[\]^_`{|}~Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class AlphanumericExample {    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{Punct}]";       //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 p{Alnum} Java regex

Maruthi Krishna
Updated on 21-Feb-2020 10:59:03

495 Views

This class matches alpha numeric characters.Example  Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class AlphanumericExample {    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{Alnum}]";       //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 p{Digit} Java regex

Maruthi Krishna
Updated on 09-Jan-2020 09:30:43

253 Views

This class matches decimal digits 0 to 9.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class DigitsExample {    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{Digit}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = ... Read More

Posix character classes p{Alpha} Java regex

Maruthi Krishna
Updated on 09-Jan-2020 09:45:18

502 Views

This class matches alphabetic characters both upper case and smaller case.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       //Regular expression to match lower case letters       String regex = "^\p{Alpha}+$";       //Getting the input data       Scanner sc = new Scanner(System.in);       System.out.println("Enter 5 input strings: ");       String input[] = new String[5];       for (int i=0; i

Posix character classes p{ASCII} Java regex.

Maruthi Krishna
Updated on 09-Jan-2020 07:42:06

591 Views

This class matches the ASCII characters i.e. \x00-\x7F.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    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{ASCII}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0; ... Read More

CharMatcher Class in Java

AmitDiwan
Updated on 20-Feb-2020 09:25:44

200 Views

The CharMatcher class determines a true or false value for any Java char value, just as Predicate does for any Object.Sr.NoMethods & Description1CharMatcher and(CharMatcher other)Returns a matcher that matches any character matched by both this matcher and other.2static CharMatcher anyOf(CharSequence sequence)Returns a char matcher that matches any character present in the given character sequence.3boolean apply(Character character)Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead.4String collapseFrom(CharSequence sequence, char replacement)Returns a string copy of the input character sequence, with each group of consecutive characters that match this matcher replaced by a single replacement character.5int countIn(CharSequence sequence)Returns the number of matching ... Read More

Java example to return a string representation of the deep contents of the array

AmitDiwan
Updated on 02-Jan-2020 11:19:29

62 Views

To return a string representation of the deep contents of the specified array −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       Object[] ob = {"One", "Two", "Three", "Four"};       System.out.println("Array elements...");       for (Object value : ob) {          System.out.println("Value = " + value);       }       System.out.println("The string representation of array is:");       System.out.println(Arrays.deepToString(ob));    } }OutputThis will produce the following output −Array elements... Value = One Value = Two Value = Three Value = Four The ... Read More

Advertisements