java - Test string in Servlet using regex -
invalid escape sequence (valid ones \b \t \n \f \r \" \' \ ):
private final string dogethtmlstringpartthree = "<td><input type=\"radio\" name=\"userinput\" value=\"\d.+\">\d / \d</td>";
double escaped accepted:
private final string dogethtmlstringpartthree = "<td><input type=\"radio\" name=\"userinput\" value=\"\\d.+\">\\d / \\d</td>";
test
private final string dogethtmlstringpartone = "<html><body><table>" + "<tr><td><h1>"; private final string dogethtmlstringparttwo = "</h1></td>" + "<form method=\"post\">" + "<input type=\"hidden\" name=\"randomdigitrange\" value=\"1\" />" + "<input type=\"hidden\" name=\"randommathematicaloperator\"" + " value=\"1\" /><input type=\"hidden\" name=\"fractionboolean\"" + " value=\""; private final string dogethtmlstringpartthree = "<td><input type=\"radio\" name=\"userinput\" value=\"\\d.+\">[0-9] / \\d</td>"; private final string dogethtmlstringpartfour = "</tr><tr><td>" + "<input type=\"submit\" value=\"submit\" " + "onclick='this.form.action=\"tobedefinedservlet\";' />" + "</td></tr></table></form></body></html>" + "<form action=\"/tobedefinedservlet\">" + "<input type=\"submit\" value=\"home\"></form>"; @test public void testbooleanfractiontrue() throws ioexception, servletexception { mockdoget(); assertequals(expecteddogethtmlstring("1 / 1 + 1 / 1", true), stringwriter.getbuffer().tostring().trim()); } public string expecteddogethtmlstring(string assignment, boolean fractionboolean) { return dogethtmlstringpartone + assignment + dogethtmlstringparttwo + "" + fractionboolean + "" + "\" />" + "\n" + dogethtmlstringpartthree + "\n" + dogethtmlstringpartfour; }
regex not work:
question
how test string in servlet using regex?
<td><input type="radio" name="userinput" value="(\d)+.(\d)+">(\d)+( ?\/? ?)(\d)+<\/td>
this i think regex looking for. didn't give examples test against regex. of course need escapes characters in java.
here example of using in java match against string:
public static void main(string[] args) throws exception { string regex = "<td><input type=\"radio\" name=\"userinput\" value=\"(\\d)+.(\\d)+\">(\\d)+( ?\\/? ?)(\\d)+<\\/td>"; string test = "<td><input type=\"radio\" name=\"userinput\" value=\"1.1\">112/2</td>"; system.out.println("does: " + test); system.out.println("match: regex pattern: " + regex); system.out.println("answer: " + test.matches(regex)); }
which gives me output:
does: <td><input type="radio" name="userinput" value="1.1">112/2</td> match: regex pattern: <td><input type="radio" name="userinput" value="(\d)+.(\d)+">(\d)+( ?\/? ?)(\d)+<\/td> answer: true
assuming trying use testing library (like junit), can use asserttrue(boolean)
methods. above example, use:
asserttrue(test.matches(regex));
Comments
Post a Comment