Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want just the word "yes" or "no" (i.e. "bayes theorem porno" and the "yesterday" ones <em>don't</em> match) then you can use <code>\b</code> as a boundary character in a regex: <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html" rel="nofollow"><code>Pattern</code> JavaDoc</a>, <a href="http://docs.oracle.com/javase/tutorial/essential/regex/bounds.html" rel="nofollow">Boundaries tutorial</a></p> <p>Assuming you're already lower-casing the input then this should work:</p> <pre><code>Pattern yes = Pattern.compile(".*\\byes\\b.*"); Pattern no = Pattern.compile(".*\\bno\\b.*"); ... bool matchesYes = yes.matcher(input).matches(); bool matchesNo = no.matcher(input).matches(); if (matchesYes == matchesNo) { ... //Do "invalid answer" here - //we either matched both (true, true) or neither (false, false) } else if (matchesYes) { ... //Do "Yes" here } else { //Else matches No ... //Do "No" here } </code></pre> <p>Test code:</p> <pre><code>private static Pattern yes = Pattern.compile(".*\\byes\\b.*"); private static Pattern no = Pattern.compile(".*\\bno\\b.*"); /** * @param args */ public static void main(String[] args) { TestMethod("yes"); //Yes TestMethod("no"); //No TestMethod("yesterday"); //Bad TestMethod("fred-no-bob"); //No TestMethod("fred'no'bob"); //No TestMethod("fred no bob"); //No TestMethod("snow"); //Bad TestMethod("I said yes"); //Yes TestMethod("yes no"); //Bad TestMethod("no yes"); //Bad } private static void TestMethod(String input) { System.out.print("Testing '" + input + "': "); bool matchesYes = yes.matcher(input).matches(); bool matchesNo = no.matcher(input).matches(); if (matchesYes == matchesNo) { System.out.println("Bad"); } else if (matchesYes) { System.out.println("Yes"); } else { System.out.println("No"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload