0
^((?!word).)*$

input:

word
test
one two
aword

output:

test
one two

Why is "aword" not there in output? I want the pattern to only match "word".

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
sruthi
  • 47
  • 3

2 Answers2

0

To remove all the occurrencies of the word word you can use the following:

inp = 'word test one two aword'

inp2 = re.sub(r'\bword\b','',inp)

inp2:

' test one two aword'

In alternative you can put your words into a list and use list.remove()

inp2 = inp.split(' ')
inp2.remove('word')
inp2 = ' '.join(inp2)

inp2:

'test one two aword'
imburningbabe
  • 742
  • 1
  • 2
  • 13
-1

The regular expression you entered is correct!

'''^((?!word).)*$'''

There must probably be an error in the subsequent functions.

Try checking your regular expressions with online tools.

Infectus
  • 11
  • 4
  • 1
    No, it is not correct. If you want to provide a genuine answer, it's your responsibility to test the code and suggest a viable solution. Please read the guidance on [answer]. – ekhumoro Oct 11 '22 at 12:42
  • Forgive me, but the regular expression that the user indicated is correct and was verified online by me personally before giving an answer, the regex should have correctly shown "aword", but given the very vague nature of the question I could not to do anything other than hypothesize a subsequent error, have I somehow erred in validating the regex myself? If so, can you kindly tell me where? I know I just signed up, but that doesn't automatically mean my answers are wrong. Thanks – Infectus Oct 12 '22 at 13:37
  • Let's see: `re.findall('^((?!word).)*$', 'word\ntest\none two\naword', re.M)` => `['t', 'o']`. Nope, the regex is definitely junk, and so is the online tool you used if it gives different output. Your answer is plainly wrong. I know this, because I went to the trouble of actually testing it myself, and I also provided a working solution in the comments to the question. – ekhumoro Oct 12 '22 at 13:53