Click here to watch on Youtube :
https://www.youtube.com/watch?v=TFkDn6Y4ZhE&list=UUhwKlOVR041tngjerWxVccw
RegexDemo.java
Output
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_multiline_flag.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_multiline_flag
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/07583a7ceb95d30e4d41a36362b049aa5d1ec520/BasicJava/RegexDemo_multiline_flag/?at=master
See also:
All JavaEE Videos Playlist
All JavaEE Videos
All JAVA EE Links
Servlets Tutorial
All Design Patterns Links
JDBC Tutorial
Java Collection Framework Tutorial
JAVA Tutorial
Kids Tutorial
https://www.youtube.com/watch?v=TFkDn6Y4ZhE&list=UUhwKlOVR041tngjerWxVccw
RegexDemo.java
Number of Matches = 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* Flags
*
*/
public class RegexDemo
{
public static void main(String[] args)
{
String inputText = "This is a dog"
+ System.getProperty("line.separator") + "this is a fox";
System.out.println(inputText);
/*
* Pattern.MULTILINE
*
* With out Pattern.MULTILINE flag The match fails because the
* matcher searches for dog at the end of the entire String
* but the dog is present at the end of the first line of the
* string.
*
* However, with the Pattern.MULTILINE flag, the same test
* will pass since the matcher now takes into account line
* terminators. So the String dog is found just before the
* line terminates, hence success:
*/
calculateMatches("dog$", inputText, Pattern.MULTILINE);
}
private static void calculateMatches(String regex, String inputText,
int flag)
{
Pattern pattern = Pattern.compile(regex, flag);
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.Matcher;
import java.util.regex.Pattern;
/**
*
* Flags
*
*/
public class RegexDemo
{
public static void main(String[] args)
{
String inputText = "This is a dog"
+ System.getProperty("line.separator") + "this is a fox";
System.out.println(inputText);
/*
* Pattern.MULTILINE
*
* With out Pattern.MULTILINE flag The match fails because the
* matcher searches for dog at the end of the entire String
* but the dog is present at the end of the first line of the
* string.
*
* However, with the Pattern.MULTILINE flag, the same test
* will pass since the matcher now takes into account line
* terminators. So the String dog is found just before the
* line terminates, hence success:
*/
calculateMatches("dog$", inputText, Pattern.MULTILINE);
}
private static void calculateMatches(String regex, String inputText,
int flag)
{
Pattern pattern = Pattern.compile(regex, flag);
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
This is a dog
this is a fox
Number of Matches = 1
this is a fox
Number of Matches = 1
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_multiline_flag.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_multiline_flag
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/07583a7ceb95d30e4d41a36362b049aa5d1ec520/BasicJava/RegexDemo_multiline_flag/?at=master
See also:
No comments:
Post a Comment