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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -