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 8925497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:42:51+00:00 2026-06-15T07:42:51+00:00

Possible Duplicate: How to escape text for regular expression in Java I have a

  • 0

Possible Duplicate:
How to escape text for regular expression in Java

I have a problem where my users have potty mouths….

To elaborate, my Android application uses Google Voice Search to return voice results and if the user has applied the setting to ‘Block offensive words’ it will return ‘go away’ as ‘g* a***’

When trying to establish what the user has said, I will often use common matching such as:

if(voiceResult.matches(someCommand)) { //do something

If the user has chosen to speak an obscenity, then I will get the following error:

java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index X

I can’t really request that all my users either don’t swear or turn off the filter, especially as from my tests Google Voice Search seems to have a dirty mind and often returns swear words in the middle of the most random sentences!

So, I’m a little lost with how to deal with this eventuality… I’ve looked for a way to ‘ignore regex’ within a string, but I drew a blank and I can’t figure out how I would dynamically escape any occurrences of * contained within the string…

At present, my only option seems to detect ‘*’ and then ask them nicely not to swear or to remove the filter!

Suggestions welcome! Unless you think they deserve a force close for their bad manners…

Please Note: ‘go away’ is not currently filtered – it was an example….

EDIT: The most simple example regex where I confirm a repeat voice request:

String userWords = "g* a***"

if(userWords.matches(userWords)) { // Then go on to compare userWords with other strings

EDIT2:

    String goAway = "g* a***";

    String goAway1 = Pattern.quote(goAway);
    String goAway2 = Pattern.quote(goAway);

    if (goAway1.matches(goAway2)) { \\ do something
  • 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-15T07:42:52+00:00Added an answer on June 15, 2026 at 7:42 am

    You can use Pattern.quote() to do the escaping for you, as found here.

    String pattern = Pattern.quote("g* a***");
    

    Will give you the following string:

    "g\* a\*\*\*"
    

    Note that those backslashes are actual characters in the string. If you wanted to create this string manually, you would use this assignment:

    String pattern = "g\\* a\\*\\*\\*";
    

    Now you can use goAway1 as a regex pattern that literally matches g* away*** (because every single character is treated as a literal). So, for instance:

    String goAway = "g* a***";
    String pattern = Pattern.quote("g* a***");
    if (goAway.matches(pattern)) { // we know that goAway was "g* a***"
    

    Of course, you cannot use the pattern to match a quoted string (like you did in your edited code snippet). What you are trying to do is the same as applying the regex

    String pattern = "g\\* a\\*\\*\\*";
    

    to this literal subject string:

    String subject = "g\\* a\\*\\*\\*";
    

    What happens? g in the pattern, matches g in the subject. Now the pattern contains an escape sequence \* which will match a literal *. But the subject string has a literal \ next. And this fails to match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to escape text for regular expression in Java Is there a
Possible Duplicate: Regular Expression to escape double quotes inside single quotes I need a
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: How to escape special characters like ' in sqlite in android I'm
Possible Duplicate: Java - escape string to prevent SQL injection Hi. I constantly am
Possible Duplicate: Java - escape string to prevent SQL injection I know php has
Possible Duplicate: How can I removing escape characters using php? I have a string
Possible Duplicate: NSString: newline escape in plist I have a plist with a string,
Possible Duplicate: Escape @ character in razor view engine how can i use '@'
Possible Duplicate: Can main function call itself in C++? I found this problem very

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.