Skip to content

Instantly share code, notes, and snippets.

@Alex-Just
Last active May 27, 2024 16:44
Show Gist options
  • Save Alex-Just/e86110836f3f93fe7932290526529cd1 to your computer and use it in GitHub Desktop.
Save Alex-Just/e86110836f3f93fe7932290526529cd1 to your computer and use it in GitHub Desktop.
Python regex to strip emoji from a string
import re
# http://stackoverflow.com/a/13752628/6762004
RE_EMOJI = re.compile('[\U00010000-\U0010ffff]', flags=re.UNICODE)
def strip_emoji(text):
return RE_EMOJI.sub(r'', text)
print(strip_emoji('🙄🤔'))
@Lakril
Copy link

Lakril commented Apr 7, 2021

Thanks for you help.

def add_space_between_emojies(text):
    '''
    >>> add_space_between_emojies('Python is fun 💚')
    'Python is fun '    
    '''
    from advertools.emoji import EMOJI
    EMOJI_PATTERN = EMOJI
    text = re.sub(EMOJI_PATTERN, r'', text)
    return text

@clichedmoog
Copy link

Sorry to say this but I think @mgaitan's regex is not perfect.
The recent emoji character includes various combinations and patterns so it would be more complex expression.
And this would be good implementation example by javascript: https://github.com/mathiasbynens/emoji-regex

@mgaitan
Copy link

mgaitan commented May 13, 2021

@clichedmoog you are totally right, everything here is a simplification

. For a complete/accurate emoji remover for python I recommend the library https://github.com/bsolomon1124/demoji which download the latest emoji specification to build the pattern. It's not super fast but it's exhaustive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment