2

Imagine you have the following String:

"badger randomword badger mushroom mushroom somethingelse"

I want to keep the words "badger" and "mushroom", and replace every other word with "mushroom":

"badger mushroom badger mushroom mushroom mushroom"

But I can't find a regex to match "everything except the words badger and mushroom". Can you help me?

4
  • Check for bader or mushroom, negate it? Commented Apr 26, 2016 at 10:50
  • 1
    regex101.com/r/aV4rE9/1 Commented Apr 26, 2016 at 10:50
  • I would be able to post a non-regex answer, would that be fine too? Regex are sometimes overpowered! Commented Apr 26, 2016 at 10:50
  • Non regex way is: " ".join([x if x == 'badger' else 'mushroom' for x in "badger randomword badger mushroom mushroom somethingelse".split()]) Commented Apr 26, 2016 at 10:57

5 Answers 5

3

This will work

(?!\bbadger\b|\bmushroom\b)\b[^\s]+\b

Regex Demo

Python Code

p = re.compile(r'(?!\bbadger\b|\bmushroom\b)\b[^\s]+\b')
test_str = "badger randomword badger mushroom mushroom somethingelse"
subst = "mushroom"
result = re.sub(p, subst, test_str)

Ideone Demo

Sign up to request clarification or add additional context in comments.

1 Comment

@EduardoAlmeida you can use the site regex101.com it has some brief explanation of what is going on in the regex
2

A simple non-regex oneliner to perform this task would be -

new_string = ' '.join(word if word == 'badger' else 'mushroom' for word in original_string.split())

I would advise against using a complex regular expression here since Beautiful Is Better Than Ugly in Python.

Comments

1

You could use re.sub with function parameter which would make the pattern really simple:

import re

s = "badger randomword badger mushroom mushroom somethingelse"
re.sub('\w+', lambda x: 'badger' if x.group(0) == 'badger' else 'mushroom', s)

Comments

0

A non-regex solution (and much easier to read) would be this:

oldstring = "badger randomword badger mushroom mushroom somethingelse"
newstring = ""

for part in oldstring.split(" "):
    if part in ["badger", "mushroom"]:
        newstring += part
    else:
        newstring += "mushroom"
    newstring += " "

newstring = newstring[:-1]

Comments

0

Try this

(?!badger\b)\b\w+\b

Regex demo

Explanation:
(?!…): Negative lookahead sample
\: Escapes a special character sample
\w: "word character": ASCII letter, digit or underscore sample
+: One or more sample

Python

import re
p = re.compile(ur'(?!badger\b)\b\w+\b')
test_str = u"badger randomword badger mushroom mushroom somethingelse"
subst = u"mushroom"

result = re.sub(p, subst, test_str)#badger mushroom badger mushroom mushroom mushroom

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.