Click here to watch on Youtube :
https://www.youtube.com/watch?v=9DuMFjsx0gA&list=UUhwKlOVR041tngjerWxVccw
RegexDemo.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* Predefined Character Classes
*
* The Java regex API also accepts predefined character classes.One
* special aspect of the Java version of this regex is the escape
* character.
*
* As we will see, most characters will start with a backslash, which
* has a special meaning in Java. For these to be compiled by the
* Pattern class – the leading backslash must be escaped i.e. \s
* becomes \\s.
*
*/
public class RegexDemo
{
public static void main(String[] args)
{
/*
* Matching white space:
*/
calculateMatches("\\s", "a c");
/*
* Matching non-white space:
*/
calculateMatches("\\S", "a c");
}
private static void calculateMatches(String regex, String inputText)
{
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inputText);
int matches = 0;
/*
* The find method keeps advancing through the input text and
* returns true for every match, so we can use it to find the
* match count as well:
*/
while (matcher.find())
{
++matches;
}
System.out.println("Number of Matches = " + matches);
}
}
import java.util.regex.Pattern;
/**
*
* Predefined Character Classes
*
* The Java regex API also accepts predefined character classes.One
* special aspect of the Java version of this regex is the escape
* character.
*
* As we will see, most characters will start with a backslash, which
* has a special meaning in Java. For these to be compiled by the
* Pattern class – the leading backslash must be escaped i.e. \s
* becomes \\s.
*
*/
public class RegexDemo
{
public static void main(String[] args)
{
/*
* Matching white space:
*/
calculateMatches("\\s", "a c");
/*
* Matching non-white space:
*/
calculateMatches("\\S", "a c");
}
private static void calculateMatches(String regex, String inputText)
{
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inputText);
int matches = 0;
/*
* The find method keeps advancing through the input text and
* returns true for every match, so we can use it to find the
* match count as well:
*/
while (matcher.find())
{
++matches;
}
System.out.println("Number of Matches = " + matches);
}
}
Output
Number of Matches = 4
Number of Matches = 2
Number of Matches = 2
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_predefined_class_s_S.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_predefined_class_s_S
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/9ca0aed85cf523c7d53f60f93dd6c7d098a3db63/BasicJava/RegexDemo_predefined_class_s_S/?at=master
See also: