Question

I have code which reads a text file and outputs the amount of TLA's in the text as a percentage out of how many lines contain text.

import re
total_lines = 0
matched_lines = 0
for line in open("sentences.txt"):
    total_lines += 1
    matched_lines += bool(re.search(r"\b[A-Z]{3}\b", line))
    matched_lines += bool(re.search(r"\b[A-Z]\\.[A-Z]\\.[A-Z]\b", line)) # DOES NOT WORK
print('{}% of sentences contain a TLA'.format(round(float(matched_lines) / total_lines * 100, 1)))

What i'm trying to do is count TLA's with full stops between them. So like now it counts WWW but i want it to count W.W.W as well.

Was it helpful?

Solution

In your re it should be \. not \\..

In Python an r outside a string makes a raw string literal. The benefit of raw string literals is that backslashes (\) are not special to Python, which means it's easier to pass through to re where they are special.

In both lines you are using raw string literals (good!), so the \b is specifying a word boundary. However, the \\. is looking for a \ followed by any character. What you want is \. which is a literal ..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top