In: Computer Science
create a regular expression in Java that defines the
following.
A "Stir" is a right brace '}' followed by zero or more
lowercase letters 'a' through 'z' and decimal digits '0'
through '9', followed by a left parenthesis '('.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Test
{
public static void main(String args[])
{
//here we are declaring the pattern for regex according to given
rules
Pattern mypattern =
Pattern.compile("[}][a-z0-9]+[(]");
// we are creating a matcher class object which lookasfor the
pattern defined in the given input string.
Matcher m1 =
mypattern.matcher("}abc897(");
//this loop will print the pattren beginning and ending of the
pattern in the given input string
while (m1.find())
{
System.out.println("In }abc897( Pattern is found from
"+m1.start()+" to "+(m1.end()-1));
}
// this will check whether the given input string matches with
the pattern exactly or not
if(Pattern.matches("[}][a-z0-9]+[(]", "{0a)")==false)
{
System.out.println("In abc897)
Pattern match not found.");
}
}
}