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

  • Home
  • SEARCH
  • 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 147801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:50:42+00:00 2026-05-11T08:50:42+00:00

I have a string in the format: t=’@abc @def Hello this part is text’

  • 0

I have a string in the format:

t='@abc @def Hello this part is text' 

I want to get this:

l=['abc', 'def']  s='Hello this part is text' 

I did this:

a=t[t.find(' ',t.rfind('@')):].strip() s=t[:t.find(' ',t.rfind('@'))].strip() b=a.split('@') l=[i.strip() for i in b][1:] 

It works for the most part, but it fails when the text part has the ‘@’. Eg, when:

t='@abc @def My email is red@hjk.com' 

it fails. The @names are there in the beginning and there can be text after @names, which may possibly contain @.

Clearly I can append initally with a space and find out first word without ‘@’. But that doesn’t seem an elegant solution.

What is a pythonic way of solving this?

  • 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. 2026-05-11T08:50:43+00:00Added an answer on May 11, 2026 at 8:50 am

    Building unashamedly on MrTopf’s effort:

    import re rx = re.compile('((?:@\w+ +)+)(.*)') t='@abc   @def  @xyz Hello this part is text and my email is foo@ba.r' a,s = rx.match(t).groups() l = re.split('[@ ]+',a)[1:-1] print l print s 

    prints:

    [‘abc’, ‘def’, ‘xyz’]
    Hello this part is text and my email is foo@ba.r


    Justly called to account by hasen j, let me clarify how this works:

    /@\w+ +/ 

    matches a single tag – @ followed by at least one alphanumeric or _ followed by at least one space character. + is greedy, so if there is more than one space, it will grab them all.

    To match any number of these tags, we need to add a plus (one or more things) to the pattern for tag; so we need to group it with parentheses:

    /(@\w+ +)+/ 

    which matches one-or-more tags, and, being greedy, matches all of them. However, those parentheses now fiddle around with our capture groups, so we undo that by making them into an anonymous group:

    /(?:@\w+ +)+/ 

    Finally, we make that into a capture group and add another to sweep up the rest:

    /((?:@\w+ +)+)(.*)/ 

    A last breakdown to sum up:

    ((?:@\w+ +)+)(.*)  (?:@\w+ +)+  (  @\w+ +)     @\w+ + 

    Note that in reviewing this, I’ve improved it – \w didn’t need to be in a set, and it now allows for multiple spaces between tags. Thanks, hasen-j!

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

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to MSDN, the Click event is called not only… May 11, 2026 at 6:54 pm
  • Editorial Team
    Editorial Team added an answer What I did in the end was to use tabbedmaxcontent.js… May 11, 2026 at 6:54 pm
  • Editorial Team
    Editorial Team added an answer What is your budget? Make sure everyone's expectations are realistic.… May 11, 2026 at 6:54 pm

Related Questions

I have a string in the format: t='@abc @def Hello this part is text'
I have a DataGridView that I'm binding to a DataTable. The DataTable is a
I'm just beginning to use Regex so bear with my terminology. I have a
I'm new to MVC, and am not following how you'd do paging and sorting
I have a string of email recipients in the format like this: DECLARE @recipients

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.