I need to fetch comma separated integers from a string of specific format using Ruby String#match method:
'text PaymentID: 12345'.match(PATTERN)[1..-1] # expected result: ['12345']
'text Payment ID: 12345'.match(PATTERN)[1..-1] # expected result: ['12345']
'text Payment id 12345'.match(PATTERN)[1..-1] # expected result: ['12345']
'text paymentid:12345'.match(PATTERN)[1..-1] # expected result: ['12345']
'text payment id: 12345'.match(PATTERN)[1..-1] # expected result: ['12345']
'text payment ID: 111,999'.match(PATTERN)[1..-1] # expected result: ['111', '999']
'text payment ID: 111, 222, 333'.match(PATTERN)[1..-1] # expected result: ['111', '222', '333']
So all spaces and ':' symbol are optional, the pattern should be case insensitive, text before payment can contain any characters.
My last variant was not good enough:
PATTERN = /payment[\s]?id[:]?[\s]?(\d+)(?:[,]?[\s]?(\d+))+/i
> 'text Payment id: 12345'.match(PATTERN)[1..-1]
=> ["1234", "5"]
> 'text Payment id: 12345, 333, 91872389'.match(PATTERN)[1..-1]
=> ["12345", "91872389"]
Any ideas on how to achieve this? Thanks in advance.
text.scan(/\d+/)? Or maybetext.scan(/(?:\G(?!\A)\s*,|payment\s?id:?)\s*\K\d+/i)?paymentword can contain any characters, including digits. Question updated, sorry. I'll test the second regex, it looks suitable for my needs.