[Solved] Ruby Regex – get persons name [closed]


While it’s better to use nokogiri, here is a simple regex:

▶ /(?<=\D)(\d+)\">([^<]+)<\/a>/ =~ \
  '<a href="https://test/builds/browse/user/b234556">John Doe</a>'
#⇒ 42
▶ $~
#⇒                                        ⇓⇓⇓⇓⇓⇓     ⇓⇓⇓⇓⇓⇓⇓⇓
#⇒ #<MatchData "234556\">John Doe</a>" 1:"234556" 2:"John Doe">

To get the number and person, use:

num, person = /(?<=\D)(\d+)\">([^<]+)<\/a>/.
  match('<a href="https://test/builds/browse/user/b234556">John Doe</a>').
  captures
#⇒ ["234556", "John Doe"]

3

solved Ruby Regex – get persons name [closed]