python - Searching for a unique word in a string -
i write program can perform multiple task on gedcom file (file hold information family tree).
i face 1 problem though; there lines this:
0 @f8@ fam 1 famc @f5@ 1 fams @f5@ now want extract code starts @ , ends @.
i used:
 if 'fam' in line:      var = line[1:6]      ... but when run program outputs lines famc , fams these words have fam inside them.
how can extract lines matching famonly? thank you.
you can using regular expression (word boundaries), , re.search():
lines = [     "0 @f8@ fam",     "1 famc @f5@",     "1 fams @f5@" ]  line in lines:     if re.search(r'\bfam\b', line):         var = line         print var a "word boundary" (\b) marks start or end of word.
we can use re.search() extract family code @ same time:
for line in lines:     search = re.search(r'@([a-z0-9]+)@\s*\bfam\b', line)     if search:         code = search.group(1)         print code