roopeshjupudi
roopeshjupudi

Reputation: 1

Ruby => How to match a string that contains #and a number example #56

Ruby => How to match a string that contains #and a number example #56

for example i have a string "my roll number is #256767"

i should match whether the string has # symbol and a number beside it

Upvotes: 0

Views: 506

Answers (2)

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

Use something like:

m = s.match(/^.*(#\d+).*$/)
unless m.nil?
  puts m[1]
end

Upvotes: 0

deefour
deefour

Reputation: 35360

Use Ruby's match

!"my roll number is #256767".match(/#\d+/).nil?

Upvotes: 2

Related Questions