Python: handle implicit value in string parsing


More often than not,  we have to handle default/implicit value in string parsing which requires special handling.

Here is an example in Python

Example: a grammar:
<line> ::= { <property> [<separator> <digits>] <separator> }
Example:
computer_id 1 port1 80 port2 88
computer_id 2 port1 port2 8080

By not specifying value, default/implicit value is 80.

   elements.split(" ");
   for i in range(1, len(elements)):
            if i+1 < len(elements) and elements[i+1].isdigit():
               process(elements[i], elements[i+1])    
            else:
               process(elements[i], 80)
Advertisements
Python: handle implicit value in string parsing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s