Found 34486 Articles for Programming

How to find the subsequence in an input sequence that matches the pattern required in Java Regular Expressions?

Fendadis John
Updated on 30-Jul-2019 22:30:24

354 Views

The find() method finds the subsequence in an input sequence that matches the pattern required. This method is available in the Matcher class that is available in the java.util.regex package.A program that demonstrates the method Matcher.find() in Java regular expressions is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("Sun");       Matcher m = p.matcher("The Earth revolves around the Sun");       System.out.println("Subsequence: Sun");       System.out.println("Sequence: The Earth revolves around the Sun");       if (m.find())     ... Read More

Working with Matcher.end() method in Java Regular Expressions

Rishi Raj
Updated on 30-Jul-2019 22:30:24

92 Views

The offset value after the last character is matched from the sequence according to the regex is returned by the method java.util.regex.Matcher.end(). This method requires no arguments. If no match occurs or if the match operation fails then the IllegalStateException is thrown.A program that demonstrates the method Matcher.end() Java regular expressions is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("(a*b)");       Matcher m = p.matcher("caaabccaab");       System.out.println("The input string is: caaabccaab");       System.out.println("The Regex is: (a*b)");   ... Read More

Use a character class in Java Regular Expressions

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

113 Views

A character class is set of characters that are enclosed inside square brackets. The characters in a character class specify the characters that can match with a character in an input string for success. An example of a character class is [a-z] which denotes the alphabets a to z.A program that demonstrates a character class in Java Regular Expressions is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("[a-z]+");       Matcher m = p.matcher("the sky is blue");       System.out.println("The input ... Read More

Use the ? quantifier in Java Regular Expressions

Arushi
Updated on 30-Jul-2019 22:30:24

131 Views

In general, the ? quantifier represents 0 or 1 occurrences of the specified pattern. For example - X? means 0 or 1 occurrences of X.The regex "t.+?m" is used to find the match in the string "tom and tim are best friends" using the ? quantifier.A program that demonstrates this is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("t.+?m");       Matcher m = p.matcher("tom and tim are best friends");       System.out.println("The input string is: tom and tim are best ... Read More

Use find() to find a subsequence in Java Regexp

Fendadis John
Updated on 30-Jul-2019 22:30:24

52 Views

The find() method finds the subsequence in an input sequence that matches the pattern required. This method is available in the Matcher class that is available in the java.util.regex packageA program that uses the find() method to find a subsequence in Java is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("cool");       Matcher m = p.matcher("Java is cool");       System.out.println("Subsequence: cool");       System.out.println("Sequence: Java is cool");       if (m.find())          System.out.println("Subsequence found"); ... Read More

A Reluctant qualifier in Java Regular Expressions

Rishi Raj
Updated on 30-Jul-2019 22:30:24

119 Views

The reluctant qualifier starts with the shortest string size as possible. If a match is found by the engine, the process continues to find more matches otherwise the engine adds a character to the searched string section and tries again. This continues until a match is obtained or the string is used up.The regex "B+?" is used to find the match in the string "SkyIsBlue".A program that demonstrates this is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       String regex = "B+?";       String str ... Read More

The extends Keyword in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

4K+ Views

An object can acquire the properties and behaviour of another object using Inheritance. In Java, the extends keyword is used to indicate that a new class is derived from the base class using inheritance. So basically, extends keyword is used to extend the functionality of the class.A program that demonstrates the extends keyword in Java is given as follows:Example Live Democlass A {    int a = 9; } class B extends A {    int b = 4; } public class Demo {    public static void main(String args[]) {       B obj = new B();     ... Read More

Is null a literal in Java?

Arushi
Updated on 30-Jul-2019 22:30:24

693 Views

The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc. but can be cast to a reference type. Also, null does not have the value 0 necessarily.A program that demonstrates null in Java is given as follows:Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = null;       System.out.println("Value of str is: " + str);    } }OutputValue of str is: nullNow let us understand the above program.In the main() method, the String str is initialized ... Read More

A greedy qualifier in Java Regular Expressions

Fendadis John
Updated on 30-Jul-2019 22:30:24

182 Views

A greedy qualifier repeats the specified token as many times as possible and then the engine backtracks and the greedy qualifier gives up matches to eventually find the required match.The regex "(\w+)(\d)(\w+)" is used to find the match in the string "EarthHas1Moon".A program that demonstrates this is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       String str = "EarthHas1Moon";       String regex = "(\w+)(\d)(\w+)";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(str);       m.find();       ... Read More

Finding all words that start with an 'a' in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

486 Views

All the words that start with a can be found in a string by using regular expressions in Java. The regular expressions are character sequences that can be used to match other strings using a specific pattern syntax. The regular expressions are available in the java.util.regex package which has many classes but the most important are Pattern class and Matcher class.A program that finds all words that start with an 'a' is using regular expressions is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) throws Exception {       String ... Read More

Advertisements