I’m working on 2 cases:
assume I have those var:
a = "hello"
b = "hello-SP"
c = "not_hello"
-
Any partial matches
I want to accept any string that has the variableainside, sobandcwould match. -
Patterned match
I want to match a string that hasainside, followed by'-', sobwould match,cdoes not.
I am having problem, because I always used the syntax /expression/ to define Regexp, so how dynamically define an RegExp on Ruby?
You can use the same syntax to use variables in a regex, so:
would match on anything that contains the value of the
avariable (at the time the expression is created!) andwould do the same, plus a hyphen, so
hello-in your example.Edit: As Wayne Conrad points out, if
acontains “any characters that would have special meaning in a regular expression,” you need to escape them. Example: