2

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!

2
  • It will help you regex101.com/r/dxds0B/1 Commented Apr 3, 2018 at 3:35
  • 1
    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. Commented Apr 3, 2018 at 10:40

1 Answer 1

9

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
Sign up to request clarification or add additional context in comments.

4 Comments

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?
I updated it to read from a file.
thanks a lot, learnt something new today :)
Your welcome. If you feel this acceptably answers your question, please close it by clicking the checkmark on the left. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.