Ruby RegEx not matching valid expression?

This should work: (\w+)(?:\s*(\+|\-|\*|\/)\s*(\w+))*$ I added some captures for backreferencing.

This should work: /^(\w+)(?:\s*(\+|\-|\*|\/)\s*(\w+))*$/ I added some captures for backreferencing.

This works for me in irb: expression =~ /^\w(\s(\+|-|\/|\*)\s\w)*$/ I tested it with your terms: irb(main):019:0> expressions = "a + a", "a + a + a", "a + a +", "a +", "a a", "a + a a" => "a + a", "a + a + a", "a + a +", "a +", "a a", "a + a a" irb(main):023:0> expressions. Each { |expression| irb(main):024:1* puts expression =~ /^\w(\s(\+|-|\/|\*)\s\w)*$/ irb(main):025:1> } 0 0 nil nil nil nil.

1 The light went on when I saw this test... I am testing with single characters but I am receiving words. I should be using \w+ – Matthew Carriere May 8 '10 at 1:21.

I should be using \w+ as I can recieved terms of more than one character... ^\w+(\s(+|-|\/|)\s\w+)$ Thanks for the responses everyone.

1 You'll still get an error because you're not escaping + and *. ^\w+(\s(\+|-|\/|\*)\s\w+)$ perhaps? – Firas Assaad May 8 '10 at 1:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions