(irb):1:REGEX_FSU_6 = /(?<MessageName>FSU)\/(?<VersionNumber>6)\s+(?<ShAirline_code>\d{2})-(?<ShAwb>\d{8})(?<ShOrigin>\S{3})(?<ShDestination>\S{3})\/(?<ShPartsCode>[TP])(?<ShPieces>\d+)(?<ShWeightCode>[KL])(?<ShWeight>\d+\.?\d*?)(<MvStatusCode>\S{3})\/(<MvAirline>\w{2})(?<MvFlightNumber>\w{1,3})\/(<MvDeparture>)\/(<MvOrigin>\w{3})(<MvDestination>\w{3})\/(?<MvPartsCode>[TP])(?<MvPieces>\d+)(?<MvWeightCode>[KL])(?<MvWeight>\d+\.?\d*?)/ (irb):2:body = 'FSU/6 123-87654321FRASVX/T1K6.4 PRE/U6702/28FEB/PRGSVX/T1K6.4' #=> "FSU/6\n123-87654321FRASVX/T1K6.4\nPRE/U6702/28FEB/PRGSVX/T1K6.4" (irb):3:data = body.match(REGEX_FSU_6) #=> nil (irb):4: data['ShAirline_code'] #=> undefined method '[]' for nil:NilClass 

Please tell (irb):3: how correctly in the string (irb):3: call the REGEX_FSU_6 constant, so that I can pull any value from the body by type as in the string (irb):4: :?

  • Here, obviously, the problem is not in the function call. Regular expression does not match body. - iKNG
  • I thought so too. I sit now understand. Just started to deal with regulars - Mikhail Lutsko

1 answer 1

 REGEX_FSU_6 = / (?<MessageName>FSU)\/(?<VersionNumber>6)\s+ (?<ShAirline_code>\d{2,3})-(?<ShAwb>\d{8})(?<ShOrigin>\S{3})(?<ShDestination>\S{3})\/(?<ShPartsCode>[TP])(?<ShPieces>\d+)(?<ShWeightCode>[KL])(?<ShWeight>\d+(\.\d+)?)\s+ (?<MvStatusCode>\S{3})\/(?<MvAirline>[\dA-Z]{2})(?<MvFlightNumber>\d{1,3})\/(?<MvDeparture>\d{1,2}[AZ]{3})\/(?<MvOrigin>[AZ]{3})(?<MvDestination>[AZ]{3})\/(?<MvPartsCode>[TP])(?<MvPieces>\d+)(?<MvWeightCode>[KL])(?<MvWeight>\d+(\.\d+)?) /x data = body.match REGEX_FSU_6 puts data["ShAirline_code"]