Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Maruthi Krishna
Page 33 of 50
Character class: subtraction - Java regular expressions
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 MoreCharacter class: intersection - Java regular expressions
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 MoreMatchResult end(int group) method in Java with examples.
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 MorePosix character classes p{Lu} Java regex\\n
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 MoreHow to print all the characters of a string using regular expression in Java?
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 MoreJava regex program to split a string at every space and punctuation.
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]"); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }OutputNumber of matches: 8The split() method of the String class accepts a value representing a ...
Read MoreList.replaceAll(UnaryOperator<E> operator) method in Java
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 { public String apply(String str) { return str.toUpperCase(); } } public class Test { public static void main(String[] args) throws CloneNotSupportedException { ArrayList list = new ArrayList(); list.add("Java"); list.add("JavaScript"); list.add("CoffeeScript"); ...
Read MoreJava regular expression program to validate an email including blank field valid as well
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 or more times.@ matches itself[a-zA-Z0-9.-] matches one character from English alphabet (both cases), digits, "." and "-" after the @ symbol\.[a-zA-Z]{2, 6} two to 6 letter for email domain after "."$ indicates the end of the sentenceExample 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest { public ...
Read MoreHow to match bold fields in a HTML script using a regular expression in Java?
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 pattern using the matcher() method.Print the matched parts of the input string using the group() method.Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { String str = "This is an example>/b> HTML script."; //Regular expression to match contents of ...
Read MoreRegular Expression E Metacharacter in Java.
The subexpression/metacharacter “\E” ends the quoting begun with \Q. 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