user2897415
user2897415

Reputation: 141

Should I be using regex in Python

I have a string like so:

'cathy is a singer on fridays'

and I want to be able to replace the fourth word with other verbs

so

'cathy is a dancer on fridays'

I assumed the right way to do this would be to use regex and stop when you reach the third whitespace but you can do groupings with regex and * which accepts any char. I can't seem to get it working.

Any advice would be useful. I am new to Python so please dont judge.Also is regex appropriate for this or should I use another method?

Thank you

Upvotes: 3

Views: 106

Answers (6)

rahtanoj
rahtanoj

Reputation: 351

Here is the solution using backreferences and the sub function from re

Documentation here

import re

msg = 'cathy is a singer on fridays'
print re.sub('(\w+) (\w+) (\w+) (\w+)', r'\1 \2 \3 dancer', msg, 1)

Output

>>> cathy is a dancer on fridays

Upvotes: 1

dckrooney
dckrooney

Reputation: 3121

If you already know the position of the word you want to replace in the string, you could simply use:

def replace_word(sentence, new_word, position):
    sent_list = sentence.split()
    sent_list[position] = new_word
    return " ".join(sent_list)

Upvotes: 0

user2555451
user2555451

Reputation:

No, Regex is not needed for this. See below:

>>> mystr = 'cathy is a singer on fridays'
>>> x = mystr.split()
>>> x
['cathy', 'is', 'a', 'singer', 'on', 'fridays']
>>> x[3] = "dancer"
>>> x
['cathy', 'is', 'a', 'dancer', 'on', 'fridays']
>>> " ".join(x)
'cathy is a dancer on fridays'

Or, more compact:

>>> mystr = 'cathy is a singer on fridays'
>>> x = mystr.split()
>>> " ".join(x[:3] + ["dancer"] + x[4:])
'cathy is a dancer on fridays'
>>>

The core principle here is the .split method of a string.

Upvotes: 2

Francesco Montesano
Francesco Montesano

Reputation: 8668

You can get what you want by splitting and joining the string after substituting the desired piece

stringlist = 'cathy is a singer on fridays'.split()
stringlist[3] = 'dancer'
print(' '.join(stringlist))

Upvotes: 1

ford prefect
ford prefect

Reputation: 7388

You can either split the string using split(' ') or a tokenizer like nltk which might also provide you some more functionality for this specific use case with part of speech analysis. If you are trying to replace it with random nouns of profession look for a word bank. Regex is overkill for what you need.

Upvotes: 0

Corley Brigman
Corley Brigman

Reputation: 12401

if you really just want the third word, split/slice/join is easier:

mytext = 'cathy is a singer on fridays'

mysplit = mytext.split(' ')
' '.join(mysplit[:3] + ['dancer',] + mysplit[4:])

regex can do much more complicated things, and there is a re.split, and there might be a faster way to do it, but this is reasonable and readable.

Upvotes: 0

Related Questions