1

I have the following text:

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

I'm trying to replace each word with user input. I am using regular expressions to match the words and get them to a list.

I am using re.sub to replace the userinput with the matched regex group. But is there a way I can only replace the first matched group, or second or third?

NOUN comes up twice, so in my for loop, I want the user input to only replace regex.group[0] or [1], for example.

finefoot
  • 9,914
  • 7
  • 59
  • 102
zzdevzz
  • 31
  • 1
  • 2
  • Could you share what you have tried so far? – Anoop R Desai Jan 01 '20 at 13:49
  • Tried various things, the re.sub method with count. (this only replaces strings including and up to a value - when i need only a specific value replaces). Tried the \1 method in re.sub (don't fully understand it) but I think that uses the groups found as part of the replacement pattern in the string. – zzdevzz Jan 01 '20 at 13:53

1 Answers1

0

edit You probably will have to iterate over all words, and make a call for each one, whether you want to change it or not. I'm not sure, how you want to treat punctuation, but I guess something along this lines should do:

(It iterates over all maximum-characters literals, which don't contain space in them)

import re

x='The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.'
y=''
for wrd in re.findall(r"[^ ]+", x):
    z=input(f"found word {wrd}, replace: ")
    if(z!=''):
        y+=f"{z} "
    else:
        y+=f"{wrd} "

print(y)

Ref: https://docs.python.org/2/library/re.html

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
  • Right, with this method I know the count goes 'up to count' replacements. But what happens if I just want to replace the third group match, without the other 2 being effected? – zzdevzz Jan 01 '20 at 13:43
  • Then you will have to just iterate over it one by one, and put old, or replaced value in a buffer. Check my edit – Grzegorz Skibinski Jan 01 '20 at 14:02
  • Ok I see your code, a think I understand it. But there's no way where you can easily replace a specific regex group by using index and targeting only that group (without count, including all the previous ones). – zzdevzz Jan 01 '20 at 14:12
  • I thought so, just googled it though, and there's something: https://stackoverflow.com/a/27589356/11610186 nevertheless if I understand your premise correctly- I would still go with breaking it down to words/literals/bits, and then modifying these one by one, making a call what to do with each one based on user input. – Grzegorz Skibinski Jan 01 '20 at 14:16
  • That helped a lot! Thanks so much man. This might sound stupid, but what did you do to get that search result. I spent an hour searching various terms (on google) and couldn't find that thread? I know most of coding is searching for problems like I have and trying to find the answer so just want to help on that part. Thanks again! – zzdevzz Jan 01 '20 at 14:50
  • :) "how to find regex replace specific occurence python" on duckduckgo.com to be specific;) I don't do Google to be honest, I just used it as a phrase;) – Grzegorz Skibinski Jan 01 '20 at 15:36