1

I have some strings like 2015 - THIS Test and 2015 - THAT Test.

I want to have the part THIS Test or THAT Test so I tried this:

"2015 - THIS Test"[/((THIS|THAT)\s\.*)/]

But that only gives me THIS or THAT.

  • Why does it cut the rest?
  • How to get the desired substring correctly?

I don't want to rely on just cutting the first 7 characters.

5
  • 1
    You escaped the dot. Remove the \. Commented Jan 19, 2016 at 13:00
  • Oh wow... I was searching for like one hour, thank you! Make this an answer Commented Jan 19, 2016 at 13:00
  • 1
    I have :) This is exactly the opposite to what mistakes people usually make: they usually forget to escape a special metacharacter. This time, it is vice versa. Commented Jan 19, 2016 at 13:03
  • I suggest using Rubular next time, as it also has a guide on how to use regex in Ruby Commented Jan 19, 2016 at 16:55
  • Thanks for that advice but I did indeed use Rubular. It worked there because I didn't escape the dot, but it seems like I somehow failed at copying it to my script... Commented Jan 19, 2016 at 22:47

1 Answer 1

3

You escaped the dot and it lost the meaning of any character but a newline and started to denote a literal . symbol. \.* matches zero or more literal dots.

Remove the \:

puts "2015 - THIS Test"[/((THIS|THAT)\s.*)/]
puts "2015 - THAT Test"[/((THIS|THAT)\s.*)/]

Result (see demo):

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

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.