4

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?

5
  • \w doesn't contain a space or a quote. Commented May 30, 2014 at 19:03
  • \w === [a-zA-Z0-9_] Commented May 30, 2014 at 19:06
  • sorry actually I meant (\\w+) instead of (\w+) Commented May 30, 2014 at 19:09
  • @user3692525, When you write \w, we assume you mean the regular expression \w, which is expressed as \\w in Java String syntax. Commented May 30, 2014 at 19:10
  • Similarly, we are writing our answers under the same assumption. Commented May 30, 2014 at 19:11

5 Answers 5

8

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.
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick answer! I just have one small question. How would I get all the groups for your regex? Thanks alot
Thanks roydukkey! One last help -- sorry to disturb you... 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?
You could try this when is (.*) birthday.
It seems you are new to regex. Here is a GREAT source! roydukkey.com/regular-expression-pocket-reference
2

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.

Comments

1

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.

4 Comments

I would tent to agree.
I actually always used to do that and got sick of so much code. I thought regex would mean fewer lines and easier to my current need.
It really depends on how you are using the regex and the needs of your program. You might take into consideration the frequency of execution, audience, and various other factors. Regex are one of my most enjoyable scripts, but they are sometimes the bane of efficiency.
I've just seen a company redo an entire project that was done using regular expressions in perl and switched it over to various java parsing techniques because the perl was lagging so much from threads doing regular expressions at the same time. It really is just a matter of finding that line of if the problem is to identify a a similar string of text or if it is a pattern that has to be recognized.
0

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

Comments

0

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

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.