How to Find 3 Immediate Words After Keyword Match Using Python

how to find 3 immediate words after keyword match using python

As others have pointed out, you need to remove the : and the ^ from the pattern. Additionally, the pattern needs to be updated to include a space.

This version creates three match groups, one for each word:

>>> s = "This movie is directed by Francis Ford Coppola. It is a great movie"
>>> match = re.search("directed by (\w+) (\w+) (\w+)",s)
>>> match.group(1)
'Francis'
>>> match.group(2)
'Ford'
>>> match.group(3)
'Coppola'

Finding the next and previous word after keyword

Use split to split on whitespace; you can then get the first/last words from each string.

>>> text =  "I currently live in Chicago but  work in DC"
>>> keywords = 'live in'
>>> before, _, after = text.partition(keywords)
>>> before.split()[-1]
'currently'
>>> after.split()[0]
'Chicago'

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

Find next word after the matching keyword in a list of strings using regex in python

You can use

import re
s = ["The job ABS is scheduled by Bob.", "The job BFG is scheduled by Alice."]
rx = re.compile(r'(?<=The job )\w+')
user = tuple(map(lambda x: x.group() or "", map(rx.search, s)))
print(user)

See the Python demo.

Alternatively, if there can be any amount of whitespace, use

rx = re.compile(r'The\s+job\s+(\w+)')
user = tuple(map(lambda x: x.group(1) or "", map(rx.search, s)))

Output:

('ABS', 'BFG')

Here, the map(rx.search, s) returns an iterator to the match data objects or Nones, and the outer map(lambda x: x.group(...) or "", ...) gets the value of the group (either the whole match with .group() or Group 1 value with .group(1)), or returns an empty string if there was no match.

How to replace next word after a specific word in python

This will help -

import re

def char_index(sentence, word_index):
sentence = re.split('(\s)',sentence) #Parentheses keep split characters
return len(''.join(sentence[:word_index*2]))

def print_secure_message(msg):
secure_words = ['--db-user', '--db-password']
# Removing extra white spaces within string
msg = re.sub(' +', ' ', msg)
cpy_msg = msg.split(" ")
for word in secure_words:
# Getting index of the word's first characters
t = re.search(word, msg)
# Getting index of the next word of the searched word's
word_index = cpy_msg.index(word)+2;
index= char_index(msg, word_index)
print(t.end(), word_index, index)
msg = msg[0:t.end() + 1] + "'****'" + msg[index - 1:]
print(''.join(msg))

Python - Search for a match for a word in a list of words in a string [closed]

In python you can easily check if a string contains another by using the in operator.

Just check the string for each keyword, and remember to make the case the same.

You can use one line

[print(x) for x in keywords if x in text.upper()]

or multiple

for x in keywords:
if x in text.upper():
print(x)

In your case the following example will output PYTHON:

text     = "The best language in the word is $python at now"
keywords = ["PYTHON","PHP","JAVA","COBOL","CPP","VB","HTML"]

[print(x) for x in keywords if x in text.upper()] #PYTHON

Have a nice day.

edit

As Malo indicated, i might be better style to pass the output to a variable and then print it after.

text     = "The best language in the word is $python at now"
keywords = ["PYTHON","PHP","JAVA","COBOL","CPP","VB","HTML"]

matches = [x for x in keywords if x in text.upper()]

for x in matches: print(x) # PYTHON

How to get a string after a specific substring?

The easiest way is probably just to split on your target word

my_string="hello python world , i'm a beginner"
print(my_string.split("world",1)[1])

split takes the word (or character) to split on and optionally a limit to the number of splits.

In this example, split on "world" and limit it to only one split.



Related Topics



Leave a reply



Submit