Reputation:
How would I detect if I have this scenario, I would be getting this inputs
3b => allow
4b => allow
55b => allow
1111bbbb => allow
num45 => no !
and if I do allow given, I wold also like to remove all characters that are not numbers
3b => 3
555B => 555
11 => 11
I have tried to check if the given input is numeric or not, but this condition is out of scope of my knowledge.
Thanks for your time and consideration.
Upvotes: 0
Views: 100
Reputation: 3067
This will look for integer + string and convert it to an integer. It will ignore a string + integer input.
input = '45num'
if input.match(/\d+[a-zA-Z]+/)
result = input.to_i
end
result => 45
Upvotes: 2
Reputation: 33908
You can use:
/\A(\d+)[a-z]*\z/i
If the expression matches your desired number will be in the first capturing group.
Example at Rubular. (It uses ^
/$
instead of \A
/\z
just for the demonstration, you should use \A
/\z
.)
Upvotes: 2
Reputation: 54984
You really want to use: str[/\A\d+/]
- This will give you the leading digits or nil.
Upvotes: 1
Reputation: 6088
if the string begins with a number
!/^[0-9]/.match(@variable).nil?
if it does, get only the number part
@variable = @variable.gsub(/[^0-9]/, '')
Upvotes: -1
Reputation: 2341
Hmm I am no regex ninja but I think you could use: ^([\d]+)
to capture JUST the number. give it a try here
Upvotes: 0