blue-sky
blue-sky

Reputation: 53916

Regex to match subsequent words

In this regex : test3\w+ I'm attempting to match the following two words after word test3 in 'test1, test2, test3 match1 match2 tester'

Here is my attempt :

import re

words = 'test1, test2, test3 match1 match2 tester'

# match the words match1 match2
# note these two words can be anything but are preceded by test3

print(re.compile(r'test3\w+').search(words).group())

How to capture the words after test3 is matched?

Words match1 match2 should be returned.

Upvotes: 0

Views: 236

Answers (2)

Austin
Austin

Reputation: 26057

You could use a regex like

test3\s(\w+)\s(\w+)\s

Explanation:

  • \s - matches any whitespace character.

  • \w - matches any alphanumeric character and the underscore.

  • + - matches one or more occurances. ( So, \w+ matches one or more alphanumeric characters).

Demo:

>>> words = 'test1, test2, test3 match1 match2 tester'

>>> match = re.search(r'test3\s(\w+)\s(\w+)\s', words)

>>> match.group(1)  # returns what is matched btw first pair of paranthesis.
match1

>>> match.group(2)  # returns what is matched btw second pair of paranthesis.
match2

Upvotes: 1

user9455968
user9455968

Reputation:

Use this regular expression:

test3\s([^\s]+)\s([^\s]+)

This will give you two groups, one with match1, one with match2.

See https://regex101.com/r/NvPExD/1

Upvotes: 0

Related Questions