Using Named Matches in the RecordX Raw-Line Parser

require 'rxraw-lineparser'

format_mask =  "(?<firstname>\\w+)(?: (?<lastname>\\w+))?"
rxp = RXRawLineParser.new(format_mask)
rxp.parse("Dan Smith") #=> [["firstname", "lastname"], ["Dan", "Smith"]]
rxp.parse("Ruth")      #=> [["firstname", "lastname"], ["Ruth", nil]]

Note: The regex switches are not usually escaped, however it has to be escaped if you run the above example from within an IRB session.

Of course you can pass in a regex e.g.

require 'rxraw-lineparser'

format_mask =  /(?<firstname>\w+)(?: (?<lastname>\w+))?/
rxp = RXRawLineParser.new(format_mask)
rxp.parse("Dan Smith") #=> [["firstname", "lastname"], ["Dan", "Smith"]]
rxp.parse("Ruth")      #=> [["firstname", "lastname"], ["Ruth", nil]]


Resources

Tags:
Source:
1908hrs.txt
Published:
20-12-2014 19:08