regex - Scan String with Ruby Regular Expression -
i attempting scan following string following regular expression:
text = %q{akdce alaska district court cm/ecfalmdce alabama middle district courtalndce } p courts = text.scan(/(ecf\w+)|(court\w+)/)
ideally, want scan text , pull text 'ecfalmdce' , 'courtalndce' regex using, trying want string starts either court or ecf followed random string of characters.
the array being returned is:
[["ecfalmdce", nil], [nil, "courtalndce"]]
what deal nil's, have more efficient way of writing regex, , have link further documentation on match groups?
your regex captures differently ecf
, court
. can create non-capture groups ?:
text.scan(/(?:ecf|court)\w+/) # => ["ecfalmdce", "courtalndce"]
edit
about non-capture groups: can use them create patterns using parenthesis without capturing pattern.
they're patterns such (?:pattern)
you can find more information on regular expressions @ http://www.regular-expressions.info/refadv.html
Comments
Post a Comment