By xvienxz2


2018-07-05 17:42:26 8 Comments

Lets say the string representing the phrase is "Holy it is changing again and again"

I want to print out the word "changing" before "again and again", but this word may be different every time. So I need to extract the word before the phrase "again and again". The phrase "holy it is" should not be extracted.

How can I do that with Python?

I thought about using Regex like here Python regex to match word before < but I'm not too sure how to code it right.

3 comments

@Ṃųỻịgǻňạcểơửṩ 2018-07-05 17:52:38

To match any word followed by "again and again", use this regex:

  • ([\w]*) again and again

If you want to include more characters, for example the apostrophe, replace [\w] with [\w'], and similarly for other characters inside the square brackets (some requires escaping).

  • Holy it is changing again and again!
  • We are going to play again, and play again and again!
  • OMG again and again!
  • Let's go again and again. Again and again we go!
  • I got roomba'd again and again (requires adding ')
  • Foo became A-B-C again and again, Bar and Baz. (requires adding the escaped hyphen)
  • More sample regexes!

To find all occurrences of that pattern, use

The regex match = re.findall("([\w']*) again and again", phrase), where ([\w']*) is any word (sequence of word characters, including the apostrophe. It returns a list of all the words followed by "again and again".

phrase = "Holy it is changing again and again!"
match = re.findall("([\w']*) again and again", phrase)
# match is ['changing']

phrase = "Going again, going again and again, and finishing again and again!"
match = re.findall("([\w']*) again and again", phrase)
# match is ['going', 'finishing']

phrase = "Defeated again and again! I got ninja'd again and again!"
match = re.findall("([\w']*) again and again", phrase)
# match is ['Defeated', "ninja'd"]

@xvienxz2 2018-07-05 22:30:41

thanks! match = re.findall("([\w']*) again and again", phrase) is perfect

@anon 2018-07-20 00:14:12

@xvienxz2 A "thank-you" comment will eventually constitute as noise. Instead, accept this answer by clicking on the checkmark. That will also give you 2 rep.

@anon 2018-07-05 18:30:54

To start off, try this regex: "([Cc]hanging) again and again", capturing the (changing) group. The additional [Cc] addresses cases where "changing" is capitalized to "Changing".

  • Holy it is changing again and again!
  • It is changing again and again, and it still changes
  • I am changing again and again and again, and still changing again and again!
  • Changing again and again and changing again and again!
  • Some more sample regexes

To use a different word, replace ([Cc]hanging) with another word. For example, to capture "going" before "again and again", use ([Gg]oing) instead.

  • We are going again and again and again!
  • Going again and again after multiple warnings will get you banned!
  • Going again and again, and going again and again, but going around in circles.
  • Some more sample regexes

To match multiple different words followed by "again and again", including different forms of the word, use union. To match "change", "changes", "changing", "changed", "going" and considering cases where the word is capitalized, the grouped part becomes ([Cc]hange|[Cc]hanges|[Cc]hanging|[Cc]hanged|[Gg]oing)

  • Holy it changed again and again!
  • It is changing again and again. Changes again and again still!
  • My score changes again and again, but now my score does not change or going anywhere!
  • Change again and again and again, just stop the change.
  • We are going and changing again and again and again!
  • Some more sample regexes

@Ṃųỻịgǻňạcểơửṩ 2019-12-04 20:47:33

This is the first part of my answer.

@keithpjolley 2018-07-05 18:13:21

import re

text = '''

Holy it is changing again and again
Holy it is not changing again and again
Holy it has changed again and again
Holy it has changed once
Holy it used to change again and again
'''

prog = re.compile(r'(\w+) again and again');
for line in text.splitlines():
  x = prog.search(line)
  if(x): print(x.group(1))

This outputs:

changing
changing
changed
change

@anon 2018-07-05 18:36:31

"word will be different every time" - can be any word.

@keithpjolley 2018-07-05 18:59:16

yeah? (\w+) matches any word.

Related Questions

Sponsored Content

67 Answered Questions

[SOLVED] How to replace all occurrences of a string?

92 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3951551 View
  • 3239 Score
  • 30 Answer
  • Tags:   python list indexing

19 Answered Questions

[SOLVED] Convert bytes to a string

3 Answered Questions

17 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

64 Answered Questions

[SOLVED] What is the difference between String and string in C#?

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

25 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

Sponsored Content