Find and extract a recurring pattern from a string

Public, Ruby General

harrylevine

Created: Dec 29, 2019     Updated: Jan 09, 2020


For example, let's say that you have a string containing any number of Slack user ids (i.e. <@W012A3CDE>), and you need to extract out just the user ids.

Here is the string you are starting with:

string = "*Message*: _Great work on that <@U03SPLX0X> , <@U93AEJ9C2> <@U6YB7VBFU> and <@U6Z3YF92P> nice job! _"

Write a regex that matches on the pattern:

/<@[^>]*>/

Use Ruby's scan method to extract the matches from the string

string.scan(/<@[^>]*>/)

#=> ["<@U03SPLX0X>", "<@U93AEJ9C2>", "<@U6YB7VBFU>", "<@U6Z3YF92P>"]