I believe you want to match a string that may contain any of the alternatives you defined in the MY_TOKENS
, then 0+ whitespaces and then 1 or more digits up to the end of the string.
Then you need to use
Regexp.new("\\A#{Regexp.union(MY_TOKENS)}?[[:space:]]*\\d+\\z").match?(s)
or
/\A#{Regexp.union(MY_TOKENS)}?[[:space:]]*\d+\z/.match?(s)
When you use a Regexp.new
, you should rememeber to double escape backslashes to define a literal backslash (e.g. "\d" is a digit matching pattern). In a regex literal notation, you may use a single backslash (/\d/
).
Do not forget to match the start of a string with \A
and end of string with \z
anchors.
Note that [...]
creates a character class that matches any char that is defined inside it: [ab]
matches an a
or b
, [program]
will match one char, either p
, r
, o
, g
, r
, a
or m
. If you have multicharacter sequences in the MY_TOKENS
, you need to remove [...]
from the pattern.
To make the regex case insensitive, pass a case insensitive modifier to the pattern and make sure you use .source
property of the Regex.union
created regex to remove flags (thanks, Eric):
Regexp.new("(?i)\\A#{Regexp.union(MY_TOKENS).source}?[[:space:]]*\\d+\\z")
or
/\A#{Regexp.union(MY_TOKENS).source}?[[:space:]]*\d+\z/i
The regex created is /(?i-mx:\Aa|b?[[:space:]]*\d+\z)/
where (?i-mx)
means the case insensitive mode is on and multiline (dot matches line breaks and verbose modes are off).