5 ms·
Also, named capture groups. Often I find my code much more readable when I name different fragments of the regex and then refer to those names later on in my co
by andrewl-hn 6y ago
Also, named capture groups. Often I find my code much more readable when I name different fragments of the regex and then refer to those names later on in my code.
- brigandish 6y agoYes, that would be my next top tip too. SHARPS_AND_FLATS = /\\^+ | _+ | =/x NOTES = /[a-g]/i p = <^ (?<pitch>#{SHARPS_AND_FLATS})? # sharps and flats (?<notes>#{NOTES}) # notes ( ,+ | '+ )? ( \\d* ) # duration ( / )? # fractions ( \\d* ) # more duration? I'd need to read the spec $>x match_data = p.match(music) match_data[:pitch] # => "#" match_data[:notes] # => "ADEADE" Much more readable.