Found 9150 Articles for Object Oriented Programming

Character class: intersection - Java regular expressions

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

314 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

99 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

76 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

84 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

309 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

How to implement LongConsumer using lambda in Java?

raja
Updated on 13-Jul-2020 13:29:18

178 Views

LongConsumer is a in-built functional interface from java.util.function package. This interface can accept a single long-valued argument as input and doesn't produce any output. It can also be used as the assignment target for a lambda expression or method reference and contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface LongConsumerExample-1import java.util.function.LongConsumer; public class LongConsumerLambdaTest {    public static void main(String[] args) {       LongConsumer displayNextVal = l-> {     // lambda expression          System.out.println("Display the next value to input : "+l);          System.out.println(l+1);       };       LongConsumer displayPrevVal ... Read More

Posix character classes Java regex

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

427 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

Posix character classes p{Lu} Java regex

Maruthi Krishna
Updated on 10-Jan-2020 12:12:39

266 Views

This class \p{Lu} matches upper case letters.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{Lu}";       //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{InGreek} Java regex

Maruthi Krishna
Updated on 21-Feb-2020 11:08:43

346 Views

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

How to print all the characters of a string using regular expression in Java?

Maruthi Krishna
Updated on 10-Jan-2020 11:58:07

2K+ Views

The meta character "." matches all the characters, to print all the characters using the regular expressions −Compile the regular expression using the compile() method.Create a Matcher object using the matcher() method.Find the matches using the find() method and for every match print the matched contents (characters) using the group() method.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       //Regular expression to match a string of non-word with length 2 to 6       String regex = ".";       Scanner sc = new ... Read More

Advertisements