2

I'm trying to get the first word in this string: Basic (11/17/2011 - 12/17/2011)

So ultimately wanting to get Basic out of that.

Other example string: Premium (11/22/2011 - 12/22/2011)

The format is always "Single-word followed by parenthesized date range" and I just want the single word.

1
  • Are you searching for that structure within an arbitrary passage of text? The current question phrasing is giving you answers for finding Basic within Basic (11/17/2011 - 12/17/2011) only. Commented Apr 2, 2012 at 16:23

7 Answers 7

5

Use this:

str = "Premium (11/22/2011 - 12/22/2011)"
str.split.first # => "Premium"

The split uses ' ' as default parameter if you don't specify any.
After that, get the first element with first

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

3 Comments

Where would the date parameter be in this method?
Like this: "Premium (11/22/2011 - 12/22/2011)".split.first
I think he is expecting variable input. How would you know the "parenthesized date range" is there?
2

You don't need regexp for that, you can just use

str.split(' ')[0]

2 Comments

This will return the first word whether it appears before a parenthesized date range or not. It also assumed the desired string appears at the beginning of the String.
What happens when the date parameter is not in the string?
1

I know you found the answer you are needing but in case anyone stumbles on this in the future, in order to pull the needed value out of a large String of unknown length:

word_you_need = s.slice(/(\b[a-zA-Z]*\b \(\d+\/\d+\/\d+ - \d+\/\d+\/\d+\))/).split[0]

3 Comments

The \b.*\b by itself matches Basic (11/17/2011 - 12/17/2011), even \b.*?\b matches Basic training.
Good point. I've changed \b.*\b to \b[a-zA-Z]*\b. So that should work unless the intended word has a hyphen or any other special character.
Why not just /(\S*\s+\(\d+\/\d+\/\d+ - \d+\/\d+\/\d+\))/ then?
0

This regular expression will match the first word with out the trailing space

"^\w+ ??"

Comments

0

If you really want a regex you can get the first group after using this regex:

(\w*) .*

4 Comments

This matches a string containing just a single space.
The only thing required in your regex is a space, everything else is optional. Therefore a string with a single space matches the space in your regex. Even though '.' will match a space, it won't match it in this case.
Oh, I thought you were saying it will not match a string with more than one space. Yes it does indeed match a string consisting of a single space. But the objective of the regex in the context of this question is to extract a part of the input string (using the parenthesis), not to validate it - the last line of the question suggests that we assume it is valid.
Indeed. You have validated a string containing just a single space and got no data in return. It the goal is to not validate, and get data instead, then \w+ will get you much further than (\w*) .*
0

"Single-word followed by parenthesized date range"

'word' and 'parenthesized date range' should be better defined
as, by your requirement statement, they should be anchors and/or delimeters.

These raw regex's are just a general guess.

\w+(?=\s*\([^)]*\))

or

\w+(?=\s*\(\s*\d+(?:/\d+)*\s*-\s*\d+(?:/\d+)*\s*\))

Comments

0

Actually, all you need is:

s.split[0]

...or...

s.split.first

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.