0

How in ruby is it possible to search text for a string that can have varying contents e.g.

text.include?("match here[generic//regex]and here").should == true

the goal is for the regex to match something to the effect of a version number, but we don't necessarily care what the version number is since the rest of the string matches.

4
  • 3
    Can you add some examples of what the regex should match and what it shouldn't? Commented Jun 16, 2014 at 16:15
  • Is this for RSpec? You may want to add the rspec tag. Commented Jun 16, 2014 at 16:18
  • input = "ruby version 1.8.7" match on = "ruby version [some numbers]". how would I match the text and acknowledge that the version exists but not a particular one. this would be in a step definition inside cucumber tests. Commented Jun 16, 2014 at 16:28
  • The wording of the question (e.g. "partial dynamic contents", "variable contents", "interpolated", etc.) does not match at all with the description of the problem. Wouldn't a simple regex with \d+ do the trick? Commented Jun 16, 2014 at 21:38

2 Answers 2

1

If you want to make sure there is a version number, and don't mind matching it, you can use:

ruby version *\d\.\d(?:\.\d)?

If you don't want to match the version number at all, you need a lookahead:

ruby version(?= *\d\.\d(?:\.\d)?)

matches ruby version but checks that we are followed by spaces and a number of the form x.x or x.x.x

Explain Regex

ruby version             # 'ruby version'
(?=                      # look ahead to see if there is:
   *                     #   ' ' (0 or more times (matching the most
                         #   amount possible))
  \d                     #   digits (0-9)
  \.                     #   '.'
  \d                     #   digits (0-9)
  (?:                    #   group, but do not capture (optional
                         #   (matching the most amount possible)):
    \.                   #     '.'
    \d                   #     digits (0-9)
  )?                     #   end of grouping
)                        # end of look-ahead

Reference

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

2 Comments

Why do you think this needs lookahead? A plain regex will do what you want, and using lookahead will make it tricky to match after the number.
@matt When I read it, I thought he didn't want to match the version number at all. Re-reading it now, I see that he doesn't mind matching it as well, so you're right, there is no need for a lookahead. Thanks for the heads up. :)
0

Here's how I'd go about it:

"ruby version"[/^ruby version [\d.]+/] # => nil
"ruby version 1.8.7"[/^ruby version [\d.]+/] # => "ruby version 1.8.7"

Now, this won't work if your version string has any alpha, beta, or similar tags:

"ruby version 1.8.7a"[/^ruby version [\d.]+/] # => "ruby version 1.8.7"

Notice that it dropped the alpha indicator. If that is significant to you, then add them to the character-set:

"ruby version 1.8.7a"[/^ruby version [\d.ab]+/] # => "ruby version 1.8.7a"

It's difficult to say for sure what you'll have to capture, since version number formats can vary wildly.

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.