Maruthi Krishna has Published 951 Articles

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

Maruthi Krishna

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

Java regex program to split a string with line endings as delimiter

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:56:00

410 Views

In windows "\r" acts as the line separator. The regular expression "\r?" matches the line endings.The split() method of the String class accepts a value representing a regular expression and splits the current string into array of tokens (words), treating the string between the occurrence of two matches as one ... Read More

Java regex program to split a string at every space and punctuation.

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:53:38

2K+ Views

The regular expression "[!._, '@?//s]" matches all the punctuation marks and spaces.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main( String args[] ) {       String input = "This is!a.sample"text, with punctuation!marks";       Pattern p = Pattern.compile("[!._, '@?//s]");     ... Read More

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

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:47:12

132 Views

The java.util.regex.MatcheResult interface provides methods to retrieve the results of a matchYou 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(int group) method of this interface accepts an ... Read More

Posix character classes p{IsLatin} Java regex

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:44:15

351 Views

This class \p{IsLatin} matches characters of Latin.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[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String ... Read More

List.replaceAll(UnaryOperator operator) method in Java

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:41:15

1K+ Views

The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation, performs the specified operation on all the elements of the current list and replaces the existing values in the list with their respective results.Example Live Demoimport java.util.ArrayList; import java.util.function.UnaryOperator; class Op implements UnaryOperator { ... Read More

Java regular expression program to validate an email including blank field valid as well

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:39:06

220 Views

Following regular expression matches given e-mail id including the blank input −^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2, 6})?$Where, ^ matches the starting of the sentence.[a-zA-Z0-9._%+-] matches one character from English alphabet (both cases), digits, "+", "_", ".", "" and, "-" before the @ symbol.+ indicates the repetition of the above mentioned set of characters one ... Read More

MatchResult groupCount() method in Java with examples.

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:34:29

88 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 groupCount() method of this interface counts and returns ... Read More

Greedy quantifiers Java Regular expressions in java.

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:25:03

398 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. Following is the list of greedy quantifiers −QuantifierDescriptionre*Matches zero or more occurrences.re?Matches zero or, 1 occurrence.re+Matches one ... Read More

How to match bold fields in a HTML script using a regular expression in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:21:29

304 Views

The regular expression "\S" matches a non-whitespace character and the following regular expression matches one or more non space characters between the bold tags."(\S+)"Therefore to match the bold fields in a HTML script you need to −Compile the above regular expression using the compile() method.Retrieve the matcher from the obtained ... Read More

Advertisements