Finding a word after a specific word in Python using regex from text file
This will do it:
import re
# Open the file for reading
with open('file.txt') as fd:
# Iterate over the lines
for line in fd:
# Capture one-or-more characters of non-whitespace after the initial match
match = re.search(r'Weather now : (\S+)', line)
# Did we find a match?
if match:
# Yes, process it
weather = match.group(1)
print('weather: {}'.format(weather))
Since what you're capturing is non-whitespace that is delimited by whitespace, you can just use \S+
.
\S
is non-whitespace--the opposite of\s
, which is whitespace+
indicates one or more of the previous character or character class
For the capture groups, group 0 corresponds to the entire regex and the captured subgroups are indexed sequentially. Since we only have one subgroup, we want group 1.
Running the above:
$ python extract.py
weather: Mildly-sunny34
Yubi
Updated on June 11, 2022Comments
-
Yubi almost 2 years
I'm very new to regex and I need to read in from a text file and find a word after a particular word + characters. For example, the content of the text file is:
Weather now : Mildly-sunny34 Weather tomorrow : Cloudy
I want to extract
"Mildly-sunny34"
after searching for the keyword"Weather now"
from the text doc. I want to make sure I do not also get":"
or" "
spaces apart from the word"Mildly-sunny34"
.Any help with some explanation is much appreciated. Thanks!
-
Vaibhav Bhavsar about 6 yearsIt will help you regex101.com/r/dxds0B/1
-
cryptoplex about 6 yearsI don't think this question is a duplicate of stackoverflow.com/questions/12572362/… for two reasons: (a) That question is asking about getting the remainder of the string after a given word, not an identified substring, and (b) This question is specifically asking how to accomplish the task with regex; only two answers of that question use regexs, and they use them in a way that doesn't work for this question.
-
-
Yubi about 6 yearsHi yes! Thanks that works with a string. However I'm having an error (TypeError: expected string or bytes-like object) when I'm trying to use the text in my text file, not sure why?
-
cryptoplex about 6 yearsI updated it to read from a file.
-
Yubi about 6 yearsthanks a lot, learnt something new today :)
-
cryptoplex about 6 yearsYour welcome. If you feel this acceptably answers your question, please close it by clicking the checkmark on the left. Thanks.