Reputation:
/[a-zA-Z]*/
The above matches all characters from a-z, but I would like to exclude when there is 'if' in the string.
Tried [^if]
but couldn't accomplish what I was hoping for.
[EDIT] I have a formula evaluator which returns some value:
value = "if(a < b,a,b+1)"
formula = value.gsub(/[a-zA-Z]*/,'1')
verify = Calc.evaluate(formula)
I am trying to assign 1
to a
and b
inside the string and check if the formula is valid and evaluated.
Expected Output: "if(1 < 1,1,1+1)"
Upvotes: 2
Views: 482
Reputation: 160551
I am trying to assign 1 to a and b inside the string...
Well, if you're trying to do that, then why are you messing with anything but a
or b
?
str = 'if(a < b,a,b+1)'
str.gsub(/\b[ab]\b/, '1') # => "if(1 < 1,1,1+1)"
\b
is a word-boundary marker, so \b[ab]\b
means a
or b
has to be an individual character.
Upvotes: 0
Reputation: 1771
To get want you want you can use gsub
like this:
"if(a < b,a,b+1)".gsub(/[a-zA-Z]+/){|m| m == 'if' ? m : '1' }
Upvotes: 0
Reputation: 44675
You can try this:
/((?!if )[a-zA-Z])*/
string = "abc"
string.match(/((?!if )[a-zA-Z])*/)
# MatchData "abc"
string = "if abc"
# MatchData ""
Upvotes: 1