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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:47:49+00:00 2026-05-29T22:47:49+00:00

I am working in Java to read a string of over 100000 characters. I

  • 0

I am working in Java to read a string of over 100000 characters.
I have a list of keywords, that I search the string for, and if the string is present I call a function which does some internal processing.

The kind of keyword I have is “face”, for example – I wish to get all the patterns where I have matches for “faces” not “facebook”. I can accept a space character behind the face in the string so if in a string I have a match like ” face” or ” faces” or “face ” or ” faces” i can accept that too. However I can not accept “duckface” or “duckface ” etc.

I have written the regex

Pattern p = Pattern.compile("\\s+"+keyword+"s\\s+|\\s+");

where keyword is my list of keywords, but I am not getting the desired results. Can you read my description and please suggest what might be issue and how I can fix it?

Also if a pointer to a really good regex for Java page is shared I would appreciate that as well.

Thank you Contributers ..

Edit

The reason I know it is not working is I have used the following code:

Pattern p = Pattern.compile("\\s+"+keyword+"s\\s+|\\s+");
            Matcher m = p.matcher(myInputDataSting);
            if(m.find())
            {
                System.out.println("Its a Match: "+m.group());
}

This returns a blank string…

  • 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-05-29T22:47:50+00:00Added an answer on May 29, 2026 at 10:47 pm

    If keyword is "face", then your current regex is

    \s+faces\s+|\s+
    

    which matches either one or more whitespace characters, followed by faces, followed by one or more whitespace characters, or one or more whitespace characters. (The pipe | has very low precedence.)

    What you really want is

    \bfaces?\b
    

    which matches a word boundary, followed by face, optionally followed by s, followed by a word boundary.

    So, you can write:

    Pattern p = Pattern.compile("\\b"+keyword+"s?\\b");
    

    (though obviously this will only work for words like face that form their plurals by simply adding s).

    You can find a comprehensive listing of Java’s regular-expression support at http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html, but it’s not much of a tutorial. For that, I’d recommend just Googling “regular expression tutorial”, and finding one that suits you. (It doesn’t have to be Java-specific: most of the tutorials you’ll find are for flavors of regular-expression that are very similar to Java’s.)

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

Sidebar

Related Questions

I am working on a short java assignment that I have been set. The
I'm trying to use java to read a string from a file that was
I have a java program that connects to a MySql database and it's working
I have been working with Java a couple of years, but up until recently
I'm working with java, trying to use the AudioFormat Class. I have the following
I have been working in Java/J2ee projects, in which I follow the Maven structure.
I have created a couple of java working sets for a project in my
I have made a simple program with : working with files(read write) end class
I have written a program in Java to read in a text file of
I am working on a java web application and I have a few questions

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.