I want to code a regex in java. The possible strings for this are:
yyyyyy$
<t>yy<\t>$
<t><t>yyyyy<\t><\t>$
<t><t>y<\t>y<\t><t>yyyyy<\t>yy$
And the strings NOT allowed or possible are:
<t><\t>$ (no “y” in the string)
<t>yy<t><\t>$ (one extra <t> ).
Some Specifications are: There is exactly one $ in any correct string, and this is always the last symbol in the string. The string before the $ must be non-empty, and we call it an expression. An expression is defined recursively as:
- the letter ‘y’
- an expression bracketed by
<t>
and<\t>
- a sequence of expressions.
The regex I have built is : y+|y*(<t>y+(<t>y*<\t>)*<\t>)
Now I am coding this regex in java as: "d+|(d*(<s>d+(<s>d*<\\s>)*<\\s>))$"
Code:
private static void checkForPattern(String input) {
Pattern p = Pattern.compile(" d+ | (d*(<s>d+(<s>d*<\\s>)*<\\s>)) $");
//Pattern p= Pattern.compile("d+|d*<s>dd<\\s>$");
Matcher m = p.matcher(input);
if (m.matches()) {
System.out.println("Correct string");
} else {
System.out.println("Wrong string");
}
}
What is the error in the syntax as it is saying "wrong" on every String that I am parsing.