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.
In your re it should be
\.not\\..In Python an
routside 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 torewhere they are special.In both lines you are using raw string literals (good!), so the
\bis specifying a word boundary. However, the\\.is looking for a\followed by any character. What you want is\.which is a literal..