I have to parse this format using regexp in TCL.
Here is the format
wl -i eth1 country
Q1 (Q1/27) Q1
I’m trying to use the word country as a keyword to parse the format ‘Q1 (Q1/27) Q1’.
I can do it if it is in a same line as country using the following regexp command.
regexp {([^country]*)country(.*)} $line match test country_value
But how can i tackle the above case?
Firstly, the regular expression you are using isn’t doing quite the right thing in the first place, because
[^country]matches a set of characters that consists of everything except the letters incountry(so it matches from thehineth1onwards only, given the need to havecountryafterwards).By default, Tcl uses the whole string to match against and newlines are just ordinary characters. (There is an option to make them special by also specifying
-line, but it’s not on by default.) This means that if I use your whole string and feed it throughregexpwith your regular expression, it works (well, you probably want tostring trim $country_valueat some point). This means that your real problem is in presenting the right string to match against.If you’re presenting lines one at a time (read from a file, perhaps) and you want to use a match against one line to trigger processing in the next, you need some processing outside the regular expression match:
If you want to skip blank lines completely, put a
if {$line eq ""} continuebefore theif {$found_country} ....