[Python] Substitution problem for 2 same instances in one string by using regular expression...

Discussão em 'Python' iniciado por Stack, Novembro 21, 2025.

  1. Stack

    Stack Membro Participativo

    There is a task that says to replace NOUN words with any noun word possible while using regex substitution but issue is that, there are two of these words and each should be replaced separately.

    The string sentence is like this: 'ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.'

    Output should be something like this: 'Cute panda walked to the pool and then jumped. A nearby giraffe was unaffected by these events.'

    As you see there are two NOUN words and each should be replaced with different words.

    My code for this is like this:

    import re

    TextFileContent ='ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.'

    SpecialRegexes_NOUN = re.compile('NOUN')

    if SpecialRegexes_NOUN.search(TextFileContent) != None:
    ForGroup2 = input('Enter a noun: ')
    ForGroup3 = input('Enter a noun: ')
    TextFileContent = re.sub('NOUN', ForGroup2, TextFileContent, 1)
    TextFileContent = re.sub('NOUN', ForGroup3, TextFileContent, 1)

    print(TextFileContent)


    When I run, get error warning saying:

    Warning (from warnings module):
    File "D:\MyPythonScripts\Mad Libs.py", line 25
    TextFileContent = re.sub('NOUN', ForGroup3, TextFileContent, 1)
    DeprecationWarning: 'count' is passed as positional argument

    Continue reading...

Compartilhe esta Página