2

Why does this not work?

re.sub('\\b[a@](\\W|[a@])*[s5$](\\W|[s5$])*[s5$](\\W|[s5$])*($|\\W)', '*', '@ss')

I do not see why @ss is not replaced by *. Similarly, @55 is not replaced.

These are replaced: a55, a5s, as5, ass

Thank you!

3
  • just clarifying that s looks like 5 and $ and 'a' looks like @ Commented Aug 16, 2012 at 23:27
  • Wouldn't re.sub(r'[a@][s5$]{2}', '*', '@ass') be much simpler and give the same result, or am I missing something? Commented Aug 16, 2012 at 23:36
  • yes, that would work. but iam writing a general regex that would work for everything :) Commented Aug 16, 2012 at 23:40

3 Answers 3

2

It's because @ is not a word character, and thus the first \b is not matched.

This is my suggestion:

re.sub('(\\ba|@)(\\W|[a@])*[s5$](\\W|[s5$])*[s5$](\\W|[s5$])*($|\\W)', '*', '@ss')

(Replacing \b[a@] with (\ba|@))

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

Comments

0

You don't have a pair of parentheses around the first section. Try this:

re.sub('(\\b[a@])*(\\W|[a@])*[s5$](\\W|[s5$])*[s5$](\\W|[s5$])*($|\\W)', '*', '@ss')

Comments

0

If you're trying a sort of "profanity" check - I would take the logic out the regex.

look_alike = {'@': 'A', '$': 'S'}
test_string = ''.join(look_alike.get(c, c) for c in your_string.upper()) # also look at `string.translate`

Then if 'ASS' in test_string - or similar with word boundaries using an re.

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.