Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:26:42+00:00 2026-06-04T04:26:42+00:00

I’m iterating through an array list of voice data to decipher if the user

  • 0

I’m iterating through an array list of voice data to decipher if the user answered ‘yes’ or ‘no’. Simple hey…

This is the initial check I have to detect an unclear response that contains both ‘yes’ and ‘no’. It works perfectly, but just looking at it, I know I should be embarrassed to post it and it can be greatly simplified!

    if ((element.toString().startsWith("yes ")
    || element.toString().endsWith(" yes")
    || element.toString().contains(" yes "))
    && (element.toString().startsWith("no ")
    || element.toString().endsWith(" no")       
    || element.toString().contains(" no "))) {

    // I heard both yes and no - inform user I don't understand

I want the user to be able to accept or decline using whatever natural speech they desire and therefore need to consider the unlikely eventualities of the following appearing in the array data:

  • yes no
  • sey on
  • yes knocking
  • yesterday no
  • yesterday knocking
  • bayes theorem porno

I’ve been through so many regular expression posts and tutorials, but no matter what I do, I cannot find a solution that works better than the posted code. The white space [\\s] being there or ‘|’ not, I can’t resolve…

I thank you in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:26:43+00:00Added an answer on June 4, 2026 at 4:26 am

    If you want just the word “yes” or “no” (i.e. “bayes theorem porno” and the “yesterday” ones don’t match) then you can use \b as a boundary character in a regex: Pattern JavaDoc, Boundaries tutorial

    Assuming you’re already lower-casing the input then this should work:

    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
    }
    

    Test 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");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.