0

I have a string:

s= "Classic for older systems. πŸ˜ƒπŸ’ People β€’ 🐻🌻 Animals β€’ πŸ”πŸΉ Food β€’ 🎷⚽ Activities β€’ πŸš˜πŸŒ‡ Travel β€’ πŸ’‘πŸŽ‰ Objects β€’ πŸ’–πŸ”£ Symbols ...45.6"

I want to remove symbols, emojis, β€’

Expected output is as follows:

"Classic for older systems  People   Animals   Food   Activities   Travel   Objects   Symbols 45.6"

Code:

re.sub(r'([^\s\w]|_)+', '', s)

produces

'Classic for older systems  People   Animals   Food   Activities   Travel   Objects   Symbols 456'

It is removing dot from floating point number. How can I fix this?

2
  • 1
    What about (?:[^\w\s.]|_|(?<!\d)\.)+ or (?:[^a-z\d.\s]|(?<!\d)\.)+ using case-insensitive flag? You can also use (\d\.\d)|[^a-z\d\s] using case-insensitive flag and replace with $1 Commented Feb 13, 2018 at 17:41
  • How do you know it's floating point instead of a dot ? I don't believe you can tell the difference. Commented Feb 13, 2018 at 17:48

2 Answers 2

2

See regex in use here

(\d+\.\d+)|[^a-z\d\s]+
  • (\d+\.\d+) Captures decimal numbers into the first capture group: One or more digits, dot, one or more digits
  • [^a-z\d\s]+ Matches one or more of any characters that are not alphanumeric or whitespace. Using the i (case-insensitive flag) this also matches uppercase variants.

Replacement: $1

Outputs:

Classic for older systems  People   Animals   Food   Activities   Travel   Objects   Symbols 45.6
Sign up to request clarification or add additional context in comments.

Comments

0

You could mimic (*SKIP)(*FAIL) with:

import re

s = "Classic for older systems. πŸ˜ƒπŸ’ People β€’ 🐻🌻 Animals β€’ πŸ”πŸΉ Food β€’ 🎷⚽ Activities β€’ πŸš˜πŸŒ‡ Travel β€’ πŸ’‘πŸŽ‰ Objects β€’ πŸ’–πŸ”£ Symbols ...45.6"

rx = re.compile(r'\d+\.\d+|(\W+)')

def replacer(match):
    if match.group(1) is not None:
        return ' ' * len(match.group(1))
    else:
        return match.group(0)

s = rx.sub(replacer, s)
print(s)

This uses a function replacer as replacement and yields

Classic for older systems     People      Animals      Food      Activities      Travel      Objects      Symbols    45.6

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.