Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Python Regex to parse multiple "word. word. word."
I'm trying to parse lines like "THIS. THAT..OTHER " so that "THIS. THAT."
is found. There can be more than one <word><dot>
separated by a space except no space after the last one. Based on the initial feedback, I've added more example lines to the lines array below.
My current results are:
re.compile('\\s?(?P<name>(?:\\w+\\.\\s(?=[\\.\\s]))+)', re.VERBOSE)
REGEX FAILED: [THIS. THAT..OTHER
Here is the current code. I have tried several different regexes trying to find one that seeks the blank space after a dot for all but the last word/dot combination.
lines = [
'THIS. THAT..OTHER ',
'THIS. THAT. ANOTHER..OTHER ',
'THIS..OTHER ',
]
pat = re.compile(r"\s?(?P<name>(?:\w+\.\s(?=[\.\s]))+)", re.VERBOSE)
print(pat)
for line in lines:
x = pat.match(line)
if x:
if hasattr(x, 'groupdict'):
print(x.groupdict())
else:
print(x.groups())
else:
print("REGEX FAILED: [{}]".format(line))
2 answers
As @ArtOfCode mentioned, it is not fully clear what you are trying to match.
From the first line in your post, you are trying to extract THIS. THAT.
out of THIS. THAT..OTHER
. The following regex achieves it:
>>> re.search('([A-Z]+\.\s?)+', 'THIS. THAT..OTHER ')
<re.Match object; span=(0, 11), match='THIS. THAT.'>
However, from your title and your last paragraph, it sounds more like you are trying to extract THIS. THAT.
out of THIS. THAT. OTHER.
, with potential white spaces at the end. In that case, this other regex can be of use:
>>> re.search('(\s?[A-Z]+\.)+(?=(\s+[A-Z]+\.?\s?)\Z)', 'THIS. THAT. OTHER. ')
<re.Match object; span=(0, 11), match='THIS. THAT.'>
If none of these expressions are what you are looking for, please clarify your question.
0 comment threads
First of all, let's understand why your regex didn't work.
The first part is \w+\.\s
, which is "one or more alpha-numeric characters" (\w+
), followed by a dot and a space (\.\s
). If the regex was only this, it would match THIS.
(the word "THIS", plus the dot and space after it).
But you also used the lookahead (?=[\.\s])
, which means that after \w+\.\s
there must be another character, and that character must be a dot or a space. But after THIS.
there's a letter "T", so the lookahead fails in this case, thus no match is found.
It also can't find a match after "THAT", because it's followed by two dots (but the regex is looking for one dot and one space). And after "OTHER", there are no dots, only spaces, so the regex can't find any matches in this string.
Not entirely clear what parts of the string you want, but anyway...
If you want the words that are followed by a dot, you could do this:
import re
lines = [ 'THIS. THAT..OTHER ' ]
pattern = re.compile(r'\w+\.')
for line in lines:
matches = pattern.findall(line)
if matches:
print(matches) # ['THIS.', 'THAT.']
else:
print("REGEX FAILED: [{}]".format(line))
findall
returns a list containing all the matches found. In this case, I'm searching for \w+\.
(one or more alphanumeric characters followed by a dot). The result is the list ['THIS.', 'THAT.']
.
But if you want a single string containing both words, then you could do:
pattern = re.compile(r'(\w+\.\s?)+')
for line in lines:
match = pattern.search(line)
if match:
print(match[0]) # THIS. THAT.
else:
print("REGEX FAILED: [{}]".format(line))
Now I'm searching for \w+\.
(alphanumeric characters followed by a dot), followed by an optional space (\s?
). And this whole thing (the word, dot and optional space) can be repeated one or more times (this is indicated by the +
in the end, which is applied to everything inside the parenthesis).
This finds the string 'THIS. THAT.'
(obtained from the Match
object returned by the search
method).
Not sure if this will work for all your cases (as you didn't specify all of them, I'm just assuming it's a "word, dot, space, word, dot" sequence), but anyway, you can make some adjustments according to your specific needs.
If you want to get the words from the beggining, you can use match
instead of search
. Or, you can also make it explicit in the expression, by using the anchor ^
(which indicates the beginning of the string):
pattern = re.compile(r'^(\w+\.\s?)+')
If you want to limit to only 2 words, just change the +
quantifier:
pattern = re.compile(r'^(\w+\.\s?){2}')
You can customize the quantity in many different ways:
-
{2}
: exactly two ocurrences -
{2,}
: two or more ocurrences (no upper limit) -
{2,5}
: at least two, at most five ocurrences
Another caveat: in Python 3, the \w
shorthand matches letters (from all languages, not only ASCII letters), digits (not restricted to arabic digits, so it'll also match characteres like ३
- DEVANAGARI DIGIT THREE) and the character _
. If you want to restrict to ASCII letters only, you could change it to:
pattern = re.compile(r'([a-zA-Z]+\.\s?)+')
And \s
matches not only spaces, but also TAB's, line breaks and many other characters (such as non-breaking spaces - see here for a more detailed explanation). If you want to match only a space, use this:
# note the space before "?"
pattern = re.compile(r'([a-zA-Z]+\. ?)+')
# ↑
# there's a space here
And so on... the possibilities are endless, and the correct one(s) will depend on the data you have and what exactly you're trying to extract from it.
After your edit, I still think that the expressions above might work. Ex:
import re
lines = [
'THIS. THAT..OTHER ',
'THIS. THAT. ANOTHER..OTHER ',
'THIS..OTHER ',
]
pattern = re.compile(r'([a-zA-Z]+\. ?)+')
for line in lines:
match = pattern.search(line)
if match:
print(match[0])
else:
print("REGEX FAILED: [{}]".format(line))
The output is:
THIS. THAT.
THIS. THAT. ANOTHER.
THIS.
Basically, the regex matches one or more ocurrences of "word followed by a dot, followed by an optional space", which seems to be what you want.
In the third case, it's getting only THIS.
, because the +
quantifier gets one or more ocurrences. If you want only 2 or more words, just change it accordingly, as previously said (such as {2,}
for "at least two", etc).
1 comment thread