user1414202
user1414202

Reputation: 436

Java - Regex Match Multiple Words

Lets say that you want to match a string with the following regex: ".when is (\w+)." - I am trying to get the event after 'when is'

I can get the event with matcher.group(index) but this doesnt work if the event is like Veteran's Day since it is two words. I am only able to get the first word after 'when is'

What regex should I use to get all of the words after 'when is'


Also, lets say I want to capture someones bday like

'when is * birthday

How do I capture all of the text between is and birthday with regex?

Upvotes: 4

Views: 12385

Answers (5)

roydukkey
roydukkey

Reputation: 3288

You could try this:

^when is (.*)$

This will find a string that starts with when is and capture everything else to the end of the line.

The regex will return one group. You can access it like so:

String line = "when is Veteran's Day.";
Pattern pattern = Pattern.compile("^when is (.*)$");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    System.out.println("group 1: " + matcher.group(1));
    System.out.println("group 2: " + matcher.group(2));
}

And the output should be:

group 1: when is Veteran's Day.
group 2: Veteran's Day.

Upvotes: 8

tmanion
tmanion

Reputation: 401

Don't use regular expressions when you don't need to!! Although the theory of regular expressions is beautiful in the thought that you can have a string do code operations for you, it is very memory inefficient for simple use cases.

If you are trying to get the word after "when is" ending by a space, you could do something like this:

String start = "when is ";
String end = " ";
int startLocation = fullString.indexOf(start) + start.length();
String afterStart = fullString.substring(startLocation, fullString.length());
String word = afterStart.substring(0, afterStart.indexOf(end));

If you know the last word is Day, you can just make end = "Day" and add the length of that string of where to end the second substring.

Upvotes: 1

merlin2011
merlin2011

Reputation: 75589

If you want to allow whitespace to be matched, you should explicitly allow whitespace.

([\w\s]+)

However, roydukkey's solution will work if you want to capture everything after when is.

Upvotes: 2

Tripp Kinetics
Tripp Kinetics

Reputation: 5459

\w only includes word characters, which doesn't include spaces. Use [\w ]+ instead.

Upvotes: 0

strmstn
strmstn

Reputation: 872

You can express this as a character class and include spaces in it: when is ([\w ]+).

Upvotes: 0

Related Questions