I am processing various text strings to extract parameters and values for use in a ‘parameters’ table. These strings vary in size and complexity, so it’s not like I can expect a consistent syntax. One rule might say “the minimum distance to the next city is > 25.0 miles”. A different rule, however, might say “the minimum distance to (a city with a population >= 25000) is 70.0 miles”. The first rule only has one parameter ‘key’ (minimum distance), while the second also has a secondary key (population).
Because I have many possible keys, I’ve set up a hash whose key is the phrase used in the rule to identify the parameter, and whose value is the actual parameter name I’m using – e.g.
` %param_h = ( "minimum distance" => "distance",
"population" => "populate"
);
`
When I process the strings I cycle through each of the keys as follows:
` foreach $param_text (keys (%param_h)) {
if ( $input_line =~ /$param_text[^d](d*.?d*)/ ) {
print " parameter is $param_h{$param_text}; value is $1n" ;
}
}
`
This makes the search non-greedy, which is fine most of the time. The problem is that in the second rule above both ‘distance’ and ‘populate’ will wind up having a value of 25000. I also cannot make the ‘distance’ search in the second rule greedy because there may be another clause after the ones I’m showing, in which case the value of 70.0 would be skipped over.
I’m beginning to suspect that this is beyond the capabilities of any ‘general’ regular expressions I might come up with – and would almost require the use of a specific regex for each case.
Any suggestions – whether for an ‘improved’ regex or an alternative approach to properly matching parameter keys and values in this type of text?
In the actual lines I’m trying to parse, the ‘nested’ parameter always ends with a particular unit of measure after the population number, so I’ve tried taking advantage of this to prepare a new regex in
%param_h; i.e.
` "distance to[^"people"]" => "distance"
`
My expectation was that this would start a non-greedy search after the first number (25000) and so find the last number (70.0). So far this has been unsuccessful. Depending on the pattern I use (and processing a longer text string) I either get the 25000 value, or I get a value after the 70.0. Either way, I don’t get the 70.0 that I want.
Rich Lemert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.