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 834487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:40:44+00:00 2026-05-15T04:40:44+00:00

I have this piece of code that finds words that begin with @ or

  • 0

I have this piece of code that finds words that begin with @ or #,

p = re.findall(r'@\w+|#\w+', str)

Now what irks me about this is repeating \w+. I am sure there is a way to do something like

p = re.findall(r'(@|#)\w+', str)

That will produce the same result but it doesn’t, it instead returns only # and @. How can that regex be changed so that I am not repeating the \w+? This code comes close,

p = re.findall(r'((@|#)\w+)', str)

But it returns [('@many', '@'), ('@this', '@'), ('#tweet', '#')] (notice the extra ‘@’, ‘@’, and ‘#’.

Also, if I’m repeating this re.findall code 500,000 times, can this be compiled and to a pattern and then be faster?

  • 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-05-15T04:40:44+00:00Added an answer on May 15, 2026 at 4:40 am

    The solution

    You have two options:

    • Use non-capturing group: (?:@|#)\w+
    • Or even better, a character class: [@#]\w+

    References

    • regular-expressions.info/Character Class and Groups

    Understanding findall

    The problem you were having is due to how findall return matches depending on how many capturing groups are present.

    Let’s take a closer look at this pattern (annotated to show the groups):

    ((@|#)\w+)
    |\___/   |
    |group 2 |     # Read about groups to understand
    \________/     # how they're defined and numbered/named
     group 1
    

    Capturing groups allow us to save the matches in the subpatterns within the overall patterns.

    p = re.compile(r'((@|#)\w+)')
    m = p.match('@tweet')
    print m.group(1)
    # @tweet
    print m.group(2)
    # @
    

    Now let’s take a look at the Python documentation for the re module:

    findall: Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

    This explains why you’re getting the following:

    str = 'lala @tweet boo #this &that @foo#bar'
    
    print(re.findall(r'((@|#)\w+)', str))
    # [('@tweet', '@'), ('#this', '#'), ('@foo', '@'), ('#bar', '#')]
    

    As specified, since the pattern has more than one group, findall returns a list of tuples, one for each match. Each tuple gives you what were captured by the groups for the given match.

    The documentation also explains why you’re getting the following:

    print(re.findall(r'(@|#)\w+', str))
    # ['@', '#', '@', '#']
    

    Now the pattern only has one group, and findall returns a list of matches for that group.

    In contrast, the patterns given above as solutions doesn’t have any capturing groups, which is why they work according to your expectation:

    print(re.findall(r'(?:@|#)\w+', str))
    # ['@tweet', '#this', '@foo', '#bar']
    
    print(re.findall(r'[@#]\w+', str))
    # ['@tweet', '#this', '@foo', '#bar']
    

    References

    • docs.python.org – Regular Expression HOWTO
      • Compiling Regular Expressions
      • Grouping | Non-capturing and Named Groups
    • docs.python.org – re module

    Attachments

    • Snippet with output on ideone.com
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have this piece of code that is giving me trouble. I know all
I have this small piece of code that basically takes a list and runs
Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have this program that should execute a piece of code base on the
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have a piece of code that the ARC converter turned into this... //
I have got a piece of code, that should countdown some number (in this
I have this piece of code in C++: ihi = y[0]>y[1] ? (inhi=1,0) :

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.