Click here to watch in Youtube :
https://www.youtube.com/watch?v=Zvo9iu_lBm0&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
What are Java Regular Expressions and how to use pattern class | Java Regex | Regex in java |
What are Java Regular Expressions and how to use pattern class | Java Regex | Regex in java |
import java.util.regex.Pattern;
public class RegexDemo1
{
public static void main(String[] args)
{
String inputCharSeq = "Peter Welcome to India.";
String regex = ".*Welcome.*";
/*
* Parameters:
*
* regex - The expression to be compiled
*
* input - The character sequence to be matched
*
* Returns:
*
* whether or not the regular expression matches on
* the input
*
*/
boolean isMatch = Pattern.matches(regex, inputCharSeq);
System.out.println("The text contains 'Welcome'? = " + isMatch);
}
}
public class RegexDemo1
{
public static void main(String[] args)
{
String inputCharSeq = "Peter Welcome to India.";
String regex = ".*Welcome.*";
/*
* Parameters:
*
* regex - The expression to be compiled
*
* input - The character sequence to be matched
*
* Returns:
*
* whether or not the regular expression matches on
* the input
*
*/
boolean isMatch = Pattern.matches(regex, inputCharSeq);
System.out.println("The text contains 'Welcome'? = " + isMatch);
}
}
Output
The text contains 'Welcome'? = true
RegexDemo2.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexDemo2
{
public static void main(String[] args)
{
String inputCharSeq = "Peter Welcome to India.";
String regex = ".*WElCoMe.*";
/*
* Parameters:
*
* regex - The expression to be compiled
* *
* flags - Match flags, a bit mask that may include
* CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE,
* CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and
* COMMENTS
*
* Returns:
*
* the given regular expression compiled into a pattern with
* the given flags
*
*/
Pattern patternObj = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
/*
* Parameters:
*
* input - The character sequence to be matched
*
* Returns:
*
* A new matcher for this pattern
*/
Matcher matcher = patternObj.matcher(inputCharSeq);
/*
* Returns:true if, and only if, the entire region sequence matches this
* matcher's pattern
*/
boolean isMatched = matcher.matches();
System.out.println("Is it a Match? = " + isMatched);
}
}
import java.util.regex.Pattern;
public class RegexDemo2
{
public static void main(String[] args)
{
String inputCharSeq = "Peter Welcome to India.";
String regex = ".*WElCoMe.*";
/*
* Parameters:
*
* regex - The expression to be compiled
* *
* flags - Match flags, a bit mask that may include
* CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE,
* CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and
* COMMENTS
*
* Returns:
*
* the given regular expression compiled into a pattern with
* the given flags
*
*/
Pattern patternObj = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
/*
* Parameters:
*
* input - The character sequence to be matched
*
* Returns:
*
* A new matcher for this pattern
*/
Matcher matcher = patternObj.matcher(inputCharSeq);
/*
* Returns:true if, and only if, the entire region sequence matches this
* matcher's pattern
*/
boolean isMatched = matcher.matches();
System.out.println("Is it a Match? = " + isMatched);
}
}
Output
Is it a Match? = true
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_what_is_regex_caseinsensitive.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_what_is_regex_caseinsensitive
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ef02c9011c18b0aa670526ba89cc11f493b9b854/BasicJava/RegexDemo_what_is_regex_caseinsensitive/?at=master
See also:
No comments:
Post a Comment