<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://teaching.healthtech.dtu.dk:443/22116/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Regex</id>
	<title>Example code - Regex - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://teaching.healthtech.dtu.dk:443/22116/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Regex"/>
	<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22116/index.php?title=Example_code_-_Regex&amp;action=history"/>
	<updated>2026-04-29T10:05:24Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22116/index.php?title=Example_code_-_Regex&amp;diff=192&amp;oldid=prev</id>
		<title>WikiSysop: Created page with &quot;__NOTOC__ == Files used in example == [http://teaching.healthtech.dtu.dk/material/22116/people.db Database (text file) of people]  == Average height of certain people == In the file there are entries like &lt;pre&gt; CPR: 230226-9781 First name: Anton Last name: Gade Height: 201 Weight: 65 Eye color: Black Blood type: A+ Children: 081154-2786 120853-1151 050354-4664 &lt;/pre&gt; The problem we want to solve is to find the average height of the people in the file with a user given fi...&quot;</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22116/index.php?title=Example_code_-_Regex&amp;diff=192&amp;oldid=prev"/>
		<updated>2025-09-03T12:03:45Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;__NOTOC__ == Files used in example == [http://teaching.healthtech.dtu.dk/material/22116/people.db Database (text file) of people]  == Average height of certain people == In the file there are entries like &amp;lt;pre&amp;gt; CPR: 230226-9781 First name: Anton Last name: Gade Height: 201 Weight: 65 Eye color: Black Blood type: A+ Children: 081154-2786 120853-1151 050354-4664 &amp;lt;/pre&amp;gt; The problem we want to solve is to find the average height of the people in the file with a user given fi...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__NOTOC__&lt;br /&gt;
== Files used in example ==&lt;br /&gt;
[http://teaching.healthtech.dtu.dk/material/22116/people.db Database (text file) of people]&lt;br /&gt;
&lt;br /&gt;
== Average height of certain people ==&lt;br /&gt;
In the file there are entries like&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CPR: 230226-9781&lt;br /&gt;
First name: Anton&lt;br /&gt;
Last name: Gade&lt;br /&gt;
Height: 201&lt;br /&gt;
Weight: 65&lt;br /&gt;
Eye color: Black&lt;br /&gt;
Blood type: A+&lt;br /&gt;
Children: 081154-2786 120853-1151 050354-4664&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The problem we want to solve is to find the average height of the people in the file with a user given first name.&lt;br /&gt;
This solution will be done &amp;#039;&amp;#039;&amp;#039;without&amp;#039;&amp;#039;&amp;#039; using regular expressions.&amp;lt;br&amp;gt;&lt;br /&gt;
Since we need data from different lines, some kind of stateful parsing has to be implemented. There are several possibilities.&lt;br /&gt;
* Assume a new (person) record always start with the CPR field.&lt;br /&gt;
* Assume that every record is separated by a newline.&lt;br /&gt;
* Assume that the order of the fields are always the same.&lt;br /&gt;
I like the first assumption. Notice how the program is easy to change to look for more keywords than just name and height in a record.&lt;br /&gt;
Notice also how we store the data immediately when the last part in the record has been found. There is no &amp;quot;extra&amp;quot; manipulation of the last record.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
# Ask for first name&lt;br /&gt;
name = input(&amp;quot;What is the first name of the people we are looking for: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
hit_count = 0&lt;br /&gt;
accumulated_height = 0&lt;br /&gt;
try:&lt;br /&gt;
    with open(&amp;quot;people.db&amp;quot;, &amp;quot;r&amp;quot;) as people_file:&lt;br /&gt;
        keyword_hits = 0&lt;br /&gt;
        for line in people_file:&lt;br /&gt;
            try:&lt;br /&gt;
                keyword, value = line.split(&amp;quot;:&amp;quot;)&lt;br /&gt;
            except ValueError:&lt;br /&gt;
                keyword, value = &amp;#039;&amp;#039;, &amp;#039;&amp;#039;&lt;br /&gt;
            if keyword == &amp;#039;CPR&amp;#039;:&lt;br /&gt;
                keyword_hits = 0&lt;br /&gt;
            elif keyword == &amp;quot;First name&amp;quot;:&lt;br /&gt;
                this_name = value.strip()&lt;br /&gt;
                keyword_hits += 1&lt;br /&gt;
            elif keyword == &amp;quot;Height&amp;quot;:&lt;br /&gt;
                this_height = int(value.strip())&lt;br /&gt;
                keyword_hits += 1&lt;br /&gt;
            if keyword_hits == 2:&lt;br /&gt;
                keyword_hits = 0&lt;br /&gt;
                if this_name == name:&lt;br /&gt;
                    hit_count += 1&lt;br /&gt;
                    accumulated_height += this_height&lt;br /&gt;
except IOError as err:&lt;br /&gt;
    print(&amp;quot;Can not read file:&amp;quot; ,filename, &amp;quot;Reason:&amp;quot;, str(err))&lt;br /&gt;
    sys.exit(1)&lt;br /&gt;
&lt;br /&gt;
if hit_count == 0:&lt;br /&gt;
    print(&amp;quot;There are no people in file file with the name:&amp;quot;, name)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;People named {} has the average height of {:.1f} cm&amp;quot;.format(name, accumulated_height/hit_count))&lt;br /&gt;
    print(&amp;quot;Number of occurrences:&amp;quot;, hit_count)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Average weight of certain people ==&lt;br /&gt;
The problem we want to solve is to find the average weight of the people in the file with a user given last name.&lt;br /&gt;
This solution will be using regular expressions.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import sys, re&lt;br /&gt;
&lt;br /&gt;
# Ask for first name&lt;br /&gt;
name = input(&amp;quot;What is the last name of the people we are looking for: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
hit_count = 0&lt;br /&gt;
accumulated_weight = 0&lt;br /&gt;
try:&lt;br /&gt;
    with open(&amp;quot;people.db&amp;quot;, &amp;quot;r&amp;quot;) as people_file:&lt;br /&gt;
        keyword_hits = 0&lt;br /&gt;
        for line in people_file:&lt;br /&gt;
            if re.search(&amp;quot;^CPR:&amp;quot;, line):&lt;br /&gt;
                keyword_hits = 0&lt;br /&gt;
            regex_obj = re.search(&amp;quot;^Last name:\s*(.+)&amp;quot;, line)&lt;br /&gt;
            if regex_obj is not None:&lt;br /&gt;
                this_name = regex_obj.group(1).strip()&lt;br /&gt;
                keyword_hits += 1&lt;br /&gt;
            regex_obj = re.search(&amp;quot;^Weight:\s*(\d+)&amp;quot;, line)&lt;br /&gt;
            if regex_obj is not None:&lt;br /&gt;
                this_weight = int(regex_obj.group(1))&lt;br /&gt;
                keyword_hits += 1&lt;br /&gt;
            if keyword_hits == 2:&lt;br /&gt;
                keyword_hits = 0&lt;br /&gt;
                if this_name == name:&lt;br /&gt;
                    hit_count += 1&lt;br /&gt;
                    accumulated_weight += this_weight&lt;br /&gt;
except IOError as err:&lt;br /&gt;
    print(&amp;quot;Can not read file:&amp;quot; ,filename, &amp;quot;Reason:&amp;quot;, str(err))&lt;br /&gt;
    sys.exit(1)&lt;br /&gt;
&lt;br /&gt;
if hit_count == 0:&lt;br /&gt;
    print(&amp;quot;There are no people in file file with the name:&amp;quot;, name)&lt;br /&gt;
else:&lt;br /&gt;
    print(&amp;quot;People named {} has the average weight of {:.1f} kg&amp;quot;.format(name, accumulated_weight/hit_count))&lt;br /&gt;
    print(&amp;quot;Number of occurrences:&amp;quot;, hit_count)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
As can be seen from the two examples you can get far with pattern matching in python without resorting to regular expressions.&lt;br /&gt;
Regexes are great, but often you can solve your problem with more simple methods.&lt;/div&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
</feed>