RegEx to match set of lines

Terry Reedy tjreedy at udel.edu
Thu Apr 5 15:01:44 EDT 2018


On 4/5/2018 11:03 AM, Steven D'Aprano wrote:
> On Thu, 05 Apr 2018 14:09:23 +0530, Prahallad Achar wrote:
> 
>> I would like to match set of lines from the below data. this data comes
>> from one of the network
> 
> Good grief! You don't need to post your entire data set! We don't need or
> want to see hundreds of lines.

+100

> Cut your data down to a SMALL representative sample. Explain how it is
> coming to you: it looks like you are reading the data in something
> similar to key:value format. Is that correct?
> 
> If so, you don't need a regex. This is not Perl, we have more than one
> tool in our toolbox and don't have to force everything to be a regex.

+10
Your bloated post will go to at least 10 archives and some unknown 1000s 
of people.

> The way I would process it would be to write a small accumulator function
> to group the lines into records, then a filter function to extract the
> records you want, and finally a function to extract the specific values
> you want from the record.

In Python, I would write a generator to yield records (groups of lines 
pertaining to one circuit.  It appears that each group starts with a 
circuid ID and ends with a blank line.  Then

for record in file:
     if wanted(record):
         process(record)

> Far more understandable and maintainable, and less fragile, than a
> horribly complex regex.

I have done this sort of thing in C, but mixing together the code for 
separating, identifying, and processing records made it less transparent 
than the above.

-- 
Terry Jan Reedy



More information about the Python-list mailing list