Praveen
Praveen

Reputation: 1851

Identify all numbers that append '#' in Ruby

I have a message(m) say "fixes #1 ~needs verification".

I want to identify all numbers that append '#'. So, i scan the text:

issue = m.scan(/[^\#][0-9]+/)

But issue is empty unless the number after # is two digits or > 9 meaning if the message is "fixes #10 ~needs verification" then my issue is 10.

What am i doing wrong here?

Upvotes: 1

Views: 66

Answers (3)

Platinum Azure
Platinum Azure

Reputation: 46183

You're negating the character class, so your regex is matching (anything that isn't #) followed by one or more digits. 2-digit numbers fit this, but one-digit numbers prefixed by # do not.

Here's what you should do instead:

issue = m.scan(/#[0-9]+/)

Or (credit to this answer):

issue = m.scan(/#\d+/)

Upvotes: 3

marcantonio
marcantonio

Reputation: 1069

Take the square brackets off of the # sign:

issue = m.scan(/^#[0-9]+/)

Upvotes: 0

Arup Rakshit
Arup Rakshit

Reputation: 118261

"fixes #1 ~needs #12verification" .scan(/#\d+/) #=> ["#1", "#12"]

Upvotes: 2

Related Questions