Pattern-Matcher-issue-with-special-characters-in-Java
Regular
Expression (Regexp) in Java
Introduction:
Regular
Expression (regexp) is extremely useful in programming, especially in
processing text files.
The
“Sun's
online Java Tutorial”
trail
on "Regular
Expressions"
is excellently written. Please read if you are new to regexe.
java.util.regex.Pattern&java.util.regex.Matcher(JDK 1.4):
Regular
expression was introduced in Java 1.4 in package
java.util.regex
.
This package contains only two classes: Pattern
and
Matcher.
-
The
Pattern
class represents a compiled regular expression. You get aPattern
object viastatic
methodPattern.compile(String regexe)
. -
The resulting
Pattern
object is used to obtain aMatcher
instance, which is used to parse the input source.
Ex: String regexe = "......"; String input = "......"; Pattern pattern Pattern.compile(regexe); Matcher matcher = pattern.matcher(input);
you can look at the javadoc of the Pattern class:
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
you can look at the javadoc of the Matcher class:
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
I
have used following Matcher Methods:
-
The
matches
method attempts to match the entire input sequence against the pattern. -
The
lookingAt
-
The
find
Example
for matches(), find(), lookingAt() methods:
package
com.ramesh.test;
import
java.util.regex.Pattern;
import
java.util.regex.Matcher;
public
class
MatchPatternAll {
public
static
void
main(String[] args)
{
String
input
= "This
is an apple. These are 33 (thirty-three) apples";
String
regexe
= "Th";
Pattern
pattern
= Pattern.compile(regexe);
Matcher
matcher
= pattern.matcher(input);
while
(matcher.find())
{
System.out.println("find():::"+matcher.find());
System.out.println("find()
found the pattern \""
+
matcher.group()
+
"\"
starting at index "
+ matcher.start()
+
"
and ending at index "
+ matcher.end());
}
System.out.println("matches()::::"+matcher.matches());
if
(matcher.matches())
{
System.out.println("matches()
found the pattern \""
+
matcher.group()
+ "\"
starting at index "
+ matcher.start()
+ "
and ending at index "
+ matcher.end());
}
else
{
System.out.println("matches()
found nothing");
}
if
(matcher.lookingAt())
{
System.out.println("lookingAt():::"+matcher.lookingAt());
System.out.println("lookingAt()
found the pattern \""
+
matcher.group()
+ "\"
starting at index "
+ matcher.start()
+ "
and ending at index "
+ matcher.end());
}
else
{
System.out.println("lookingAt()
found nothing");
}
}
}
Output: find():::true
find()
found the pattern "Th" starting at index 18 and ending at
index 20
matches()::::false
matches()
found nothing
lookingAt():::true
lookingAt() found the pattern "Th" starting at index 0 and ending at index 2
Following
is my requirement:
Handling Regular expression all special characters in Real time Java project:
I am trying to create an application that matches a message template with a message that a user is trying to send.
I am using Java regex for matching the message. The template/message may contain special characters.
I have faced Following Match Pattern issues:
Example 1:
package
com.ramesh.List;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
MatchPattern {
public
static
void
main(String[] args)
{
String input
="m*u*r*";
String
pattern
= "m*u*r*";
Pattern p
= Pattern.compile(pattern);
Matcher m
= p.matcher(input);
boolean
b
= m.matches();
System.out.println(b);
}
}
Output: false
If
input contains any special-character it gives output as “false”.
But
user’s point of view the correct answer is “True”.
Example 2:
package
com.ramesh.List;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
MatchPattern {
public
static
void
main(String[] args)
{
String input
="mu";
String
pattern
= "m*u*r*";
Pattern p
= Pattern.compile(pattern);
Matcher m
= p.matcher(input);
boolean
b
= m.matches();
System.out.println(b);
}
}
Output: true
It
doesn’t follow the Pattern,
but
It gives the output as “true”.
But
user’s point of view the correct answer is “false”.
My Solution:
I want to write a simple regexp to check if in given string exist any special character. My regexp works and checks all the string, all special characters in user input and it in any part of the string. Below is working fine for any special character, String:
package
com.ramesh.test;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
MatchPattern {
public
boolean
containsPattern(String input,
String pattern)
{
if
(input
== null
|| input.trim().isEmpty())
{
System.out.println("Incorrect
format of string");
return
false;
}
//
“*”is replaced by “.*” in replaceAll()
//“.*”
Use this pattern to match any number, any string (including the empty
string) of any characters.
String
inputpattern
= pattern.replaceAll("\\*",
".*");
Pattern
p
= Pattern.compile(inputpattern);
Matcher
m
= p.matcher(input);
boolean
b
= m.matches();
return
b;
}
public
boolean
containsPatternNot(String input1,
String pattern1)
{
return
(containsPattern(input1,
pattern1)
== true
? false
: true);
}
public
static
void
main(String[] args)
{
MatchPattern
m1
= new
MatchPattern();
boolean
a
= m1.containsPattern("ma5&*%u&^()k5.r5gh^",
"m*u*r*");
System.out.println(a);//
returns True
boolean
d
= m1.containsPattern("mur",
"m*u*r*");
System.out.println(d);//
returns True
boolean
c
= m1.containsPatternNot("ma5&^%u&^()k56r5gh^",
"m*u*r*");
System.out.println(c);//
returns false
boolean
e
= m1.containsPatternNot("mur",
"m*u*r*");
System.out.println(e);//
returns false
}
}
Output: true true false false
Thanks, Ramesh B, ramesh18492@gmail.com, MouriTech PVT LTD, www.mouritech.com.
Comments
Post a Comment