0

I want to find the occurrences of all the words in a ListArray comparing it with a String. So far, I am able to do it as a for loop, where I store all the possible combinations and run them using a matches i.e.

        for(String temp_keywords: keywords){
        final_keywords_list.add(" "+ temp_keywords+ " ");
        final_keywords_list.add(" "+ temp_keywords+".");
        final_keywords_list.add(" "+ temp_keywords+ ",");
        final_keywords_list.add(" "+ temp_keywords+ "!");
        final_keywords_list.add(" "+ temp_keywords+ "/");
        final_keywords_list.add(" "+ temp_keywords+ "?");
    }
    for (String temp_keywords : final_keywords_list) {
            String add_space = temp_keywords.toLowerCase();
            p = Pattern.compile(add_space);
            m = p.matcher(handler_string);
            int count = 0;
            while (m.find()) {
                count += 1;
            }

However, I want to remove the manual addition for the combinations and do a regex. I've seen examples of words with regex but how do I add a variable string to the regex? Sorry, I am a beginner java learner.

2
  • you aren't using final_keywords_list in your code. Are you supposed to match values with temp_keywords or with final_keywords_list? Commented Mar 25, 2014 at 6:39
  • Yes, sorry, I will have to do that. Sorry, this is a part of a large code. I'll edit it now! Commented Mar 25, 2014 at 6:43

1 Answer 1

1

Is this what you need?

String inputString = ....
String[] keywords = ....

StringBuilder sb = new StringBuilder();
for(String keyword: keywords)
  sb.append("(?<= )").append(keyword).append("(?=[ .,!/?])").append("|");
sb.setLength(sb.length() - 1); //Removes trailing "|". Assumes keywords.size() > 0.

Pattern p = Pattern.compile(sb.toString());
Matcher m = p.matcher(inputString);
int count = 0;
while (m.find())
  count++;

It creates a single regex, compiles it, and then counts the matches.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! StringBuilder is what I was looking for! I was simply trying to concat the strings and the regex part and it obviously didn't work. I shall read up on StringBuilder.
Sorry, spaces between words of keywords? As in, if the keyword was 'bread and'?
@PR: Before my fix, it didn't count correctly " hello world " as 2. Now I'm using positive lookahead/lookbehind, so it works fine.
Please note that when the input string terminates with one of your words it will also fail to count it, but that's also true with your original solution so I assumed you don't need it.
Awesome. I didn't think of that drawback! And true, I don't need it for my code. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.