Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:33:59+00:00 2026-06-18T09:33:59+00:00

I would like to have a regex pattern to match smileys :) ,:( .Also

  • 0

I would like to have a regex pattern to match smileys “:)” ,”:(” .Also it should capture repeated smileys like “:) :)” , “:) :(” but filter out invalid syntax like “:( (” .

I have this with me, but it matches “:( (”

bool( re.match("(:\()",str) ) 

I maybe missing something obvious here, and I’d like some help for this seemingly simple task.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:34:00+00:00Added an answer on June 18, 2026 at 9:34 am

    I think it finally “clicked” exactly what you’re asking about here. Take a look at the below:

    import re
    
    smiley_pattern = '^(:\(|:\))+$' # matches only the smileys ":)" and ":("
    
    def test_match(s):
        print 'Value: %s; Result: %s' % (
            s,
            'Matches!' if re.match(smiley_pattern, s) else 'Doesn\'t match.'
        )
    
    should_match = [
        ':)',   # Single smile
        ':(',   # Single frown
        ':):)', # Two smiles
        ':(:(', # Two frowns
        ':):(', # Mix of a smile and a frown
    ]
    should_not_match = [
        '',         # Empty string
        ':(foo',    # Extraneous characters appended
        'foo:(',    # Extraneous characters prepended
        ':( :(',    # Space between frowns
        ':( (',     # Extraneous characters and space appended
        ':(('       # Extraneous duplicate of final character appended
    ]
    
    print('The following should all match:')
    for x in should_match: test_match(x);
    
    print('')   # Newline for output clarity
    
    print('The following should all not match:')
    for x in should_not_match: test_match(x);
    

    The problem with your original code is that your regex is wrong: (:\(). Let’s break it down.

    The outside parentheses are a “grouping”. They’re what you’d reference if you were going to do a string replacement, and are used to apply regex operators on groups of characters at once. So, you’re really saying:

    • ( begin a group
      • :\( … do regex stuff …
    • ‘)’ end the group

    The : isn’t a regex reserved character, so it’s just a colon. The \ is, and it means “the following character is literal, not a regex operator”. This is called an “escape sequence”. Fully parsed into English, your regex says

    • ( begin a group
      • : a colon character
      • \( a left parenthesis character
    • ) end the group

    The regex I used is slightly more complex, but not bad. Let’s break it down: ^(:\(|:\))+$.

    ^ and $ mean “the beginning of the line” and “the end of the line” respectively. Now we have …

    • ^ beginning of line
      • (:\(|:\))+ … do regex stuff …
    • $ end of line

    … so it only matches things that comprise the entire line, not simply occur in the middle of the string.

    We know that ( and ) denote a grouping. + means “one of more of these”. Now we have:

    • ^ beginning of line
    • ( start a group
      • :\(|:\) … do regex stuff …
    • ) end the group
    • + match one or more of this
    • $ end of line

    Finally, there’s the | (pipe) operator. It means “or”. So, applying what we know from above about escaping characters, we’re ready to complete the translation:

    • ^ beginning of line
    • ( start a group
      • : a colon character
      • \( a left parenthesis character
    • | or
      • : a colon character
      • \) a right parenthesis character
    • ) end the group
    • + match one or more of this
    • $ end of line

    I hope this helps. If not, let me know and I’ll be happy to edit my answer with a reply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to match by regex strings of 01 which have odd length.
I would like to match url pattern that has optional segments. I have URL-s
I have a binary file that I would like to regex search/replace hex bytes
I have some files that I would like to rename using regex and powershell,
I would like to use a symbols in a RegEx pattern to find matches,
I have a regex in PHP to match some text like this: 24th Meeting
I have the following string, which matches the following RegEx string. I would like
I have a regex to match middle names that looks like this : first_name
So in my project i would like have a nice treeview that has images.
I would like to have an AppWidget that designed like this one . Image:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.