1

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ ):

private final String doGetHtmlStringPartThree = "<td><input type=\"radio\" name=\"userInput\" value=\"\d.+\">\d / \d</td>";

Double escaped to be 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 does not work:

enter image description here

Question

How to test a String in a servlet using regex?

030
  • 10,842
  • 12
  • 78
  • 123
  • possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Paul Vargas Jun 27 '14 at 19:27
  • `.+` will match anything as much as it can. If dot-all it matches to the end of the string, if not, up to end of line. This is done first, then backtracking begins 1 char at a time from the end until it matches the rest of the expression. –  Jun 27 '14 at 20:57

1 Answers1

1
<td><input type="radio" name="userInput" value="(\d)+.(\d)+">(\d)+( ?\/? ?)(\d)+<\/td>

This I think is the regex you are looking for. You didn't give us any examples to test against the regex. You of course need to escapes the characters in java.

Example in Regexr here.

Here is an example of using this in Java to match against a 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: the regex pattern: " + regex);
    System.out.println("Answer: " + test.matches(regex));

}

Which gives me the output:

Does: <td><input type="radio" name="userInput" value="1.1">112/2</td>
Match: the regex pattern: <td><input type="radio" name="userInput" value="(\d)+.(\d)+">(\d)+( ?\/? ?)(\d)+<\/td>
Answer: true

Assuming that you are trying to use a testing library (like JUnit), you can use the assertTrue(boolean) methods. From my above example, you could use:

assertTrue(test.matches(regex));
Mike Elofson
  • 2,017
  • 1
  • 10
  • 16
  • Examples are included in the picture – 030 Jun 27 '14 at 19:32
  • I updated my answer. It's easier if you give me details on what you are asking, and what you have tried. "How to test a String in a servlet using regex?" is a very vague question, but could be solved with a quick google search. Your issue appears to be with your regex itself (I believe?). Also, putting examples in code brackets (instead of images) makes it easier for us to help. All I can see is a small portion of what you are comparing in your image. – Mike Elofson Jun 27 '14 at 19:56
  • I've updated my answer again to show how to use this regex match in Java (including how to properly escape the characters). Let me know if this is what you are looking for as an answer, or if I can help more. – Mike Elofson Jun 27 '14 at 20:01
  • The provided regex evaluates to true if `assertTrue` is used, while `assertEquals` evaluates to false. I will use the provided regex and `assertTrue` instead off `assertEquals`. This answers the question. Thank you. I will upvote and accept the answer. – 030 Jun 27 '14 at 21:30