0

I have a problem in Regular expression. I need to print and count the words starts with word ‘ge’ and end with either word ‘ne’ or ‘me’. When I running the code only words start with "ge" appear. Can anyone help me to improve my source code?

Pattern p = Pattern.compile("ge\\s*(\\w+)");
Matcher m = p.matcher(input);

int count=0;
List<String> outputs = new ArrayList<String>();

while (m.find()) {
    count++;
    outputs.add(m.group());
    System.out.println(m.group());
}

System.out.println("The count is " + count);

2 Answers 2

1
\bge\w*[nm]e\b

This should do it for you.

In java this would be

\\bge\\w*[nm]e\\b

use \b to denote word boundary

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

Comments

0

Well you're kinda missing the second half of your regex

ge\s*(\w+)[nm]e

This will work.

Regex101

Comments

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.