Finding a word after a specific word in Python using regex from text file

18,051

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
Share:
18,051
Yubi
Author by

Yubi

Updated on June 11, 2022

Comments

  • Yubi
    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
      Vaibhav Bhavsar about 6 years
      It will help you regex101.com/r/dxds0B/1
    • cryptoplex
      cryptoplex about 6 years
      I 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
    Yubi about 6 years
    Hi 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
    cryptoplex about 6 years
    I updated it to read from a file.
  • Yubi
    Yubi about 6 years
    thanks a lot, learnt something new today :)
  • cryptoplex
    cryptoplex about 6 years
    Your welcome. If you feel this acceptably answers your question, please close it by clicking the checkmark on the left. Thanks.