Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding words after keyword in python

I want to find words that appear after a keyword (specified and searched by me) and print out the result. I know that i am suppose to use regex to do it, and i tried it out too, like this:

import re s = "hi my name is ryan, and i am new to python and would like to learn more" m = re.search("^name: (\w+)", s) print m.groups() 

The output is just:

"is" 

But I want to get all the words and punctuations that comes after the word "name".

like image 770
Ryan Avatar asked Jul 09 '11 08:07

Ryan


People also ask

How do I extract a specific word from a string in Python?

Method #1 : Using split() Using the split function, we can split the string into a list of words and this is the most generic and recommended method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.

What is match and search in Python?

The re.search() and re. match() both are functions of re module in python. These functions are very efficient and fast for searching in strings. The function searches for some substring in a string and returns a match object if found, else it returns none. There is a difference between the use of both functions.

How to extract all the words that are keywords in Python?

Given List of strings, extract all the words that are keywords. Explanation : All strings in result list is valid Python keyword. Explanation : try is used in try/except block, hence a keyword. This is one of the ways in which this task can be performed. In this, we check for keyword using iskeyword () and convert a string to words using split ().

Is all strings in result list is valid Python keyword?

Explanation : All strings in result list is valid Python keyword. Explanation : try is used in try/except block, hence a keyword. This is one of the ways in which this task can be performed. In this, we check for keyword using iskeyword () and convert a string to words using split ().

What are keywords and keywords?

Keywords are the terms that represent the most relevant information contained in the document. Methods for automatic keyword extraction can be supervised, semi-supervised, or unsupervised. In research & news articles, keywords form an important component since they provide a concise representation of the article’s content.

How to extract word from text in Python using regular expression?

Extract word from your text data using Python’s built in Regular Expression Module. Regular expression (RegEx) is an extremely powerful tool for processing and extracting character patterns from text. Regular Expressions are fast and helps you to avoid using unnecessary loops in your program to match and extract desired information.


2 Answers

Instead of using regexes you could just (for example) separate your string with str.partition(separator) like this:

mystring =  "hi my name is ryan, and i am new to python and would like to learn more" keyword = 'name' before_keyword, keyword, after_keyword = mystring.partition(keyword) >>> before_keyword 'hi my ' >>> keyword 'name' >>> after_keyword ' is ryan, and i am new to python and would like to learn more' 

You have to deal with the needless whitespaces separately, though.

like image 80
Aufwind Avatar answered Sep 18 '22 13:09

Aufwind


Your example will not work, but as I understand the idea:

regexp = re.compile("name(.*)$") print regexp.search(s).group(1) # prints " is ryan, and i am new to python and would like to learn more" 

This will print all after "name" and till end of the line.

like image 32
DominiCane Avatar answered Sep 16 '22 13:09

DominiCane