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!

kiyah
  • 1,502
  • 2
  • 18
  • 27
Yubi
  • 61
  • 1
  • 1
  • 6
  • It will help you https://regex101.com/r/dxds0B/1 – Vaibhav Bhavsar Apr 03 '18 at 03:35
  • 1
    I don't think this question is a duplicate of https://stackoverflow.com/questions/12572362/get-a-string-after-a-specific-substring 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. – cryptoplex Apr 03 '18 at 10:40

1 Answers1

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
cryptoplex
  • 1,235
  • 10
  • 14
  • 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? – Yubi Apr 03 '18 at 03:57
  • I updated it to read from a file. – cryptoplex Apr 03 '18 at 04:07
  • thanks a lot, learnt something new today :) – Yubi Apr 03 '18 at 05:20
  • Your welcome. If you feel this acceptably answers your question, please close it by clicking the checkmark on the left. Thanks. – cryptoplex Apr 03 '18 at 13:47