regex - How do I extract two center columns from a tab-delimited line of text? -
i need 2 regex regular expressions. 1 find second block of numbers , 1 find third block of numbers. data this:
8782910291827182 04 1988 081
one code find 04
, other find 1988
. have expression find first 16 numbers , last 3 numbers, stuck in finding 2 numbers of second , third section.
use field-splitting instead
based on corpus, seems 1 should able rely on existence of 4 fields separated tabs or other whitespace. splitting fields easier building , testing regex, i'd recommend skipping regex unless there edge cases not included in examples.
consider following ruby examples:
# split string fields. string = '8782910291827182 04 1988 081' fields = string.split /\s+/ #=> ["8782910291827182", "04", "1988", "081"] # access members of field array. fields.first #=> "8782910291827182" fields[1] #=> "04" fields[2] #=> "1988" # unpack array elements variables. field1, field2, field3, field4 = fields p field2, field3 #=> ["04", "1988"]
a regular expression force spend more time on pattern matching, corpus grows more complex; string-splitting simpler, , enable focus more on result set. in cases, end results functionally similar, 1 more useful depend on you're trying do. it's have alternative options!
Comments
Post a Comment