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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:07:44+00:00 2026-06-08T04:07:44+00:00

Consider a string s = aa,bb11,22 , 33 , 44,cc , dd . I

  • 0

Consider a string s = "aa,bb11,22 , 33 , 44,cc , dd ".

I would like to split s into the following list of tokens using the regular expressions module in Python, which is similar to the functionality offered by Perl:

  1. "aa,bb11"
  2. "22"
  3. "33"
  4. "44,cc , dd "

Note:

  • I want to tokenise on commas, but only if those commas have numbers to either side.
  • Any (optional) whitespace around these “numerical commas” that I’m targeting should be removed in the result. The optional whitespace may be more than a single space.
  • Any other whitespace should be left as it appears in the original string.

My best attempt so far is the following:

import re

pattern = r'(?<=\d)(\s*),(\s*)(?=\d)'
s = 'aa,bb11,22 , 33 , 44,cc , dd '

print re.compile(pattern).split(s)

but this prints:

['aa,bb11', '', '', '22', ' ', ' ', '33', ' ', ' ', '44,cc , dd ']

which is close to what I want, inasmuch as the 4 things I want are contained in the list. I could go through and get rid of any empty strings and any strings that consist of only spaces/commas, but I’d rather have a single line regex that does all this for me.

Any ideas?

  • 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-08T04:07:46+00:00Added an answer on June 8, 2026 at 4:07 am

    Don’t put capture groups on the \s*:

    pattern = r'(?<=\d)\s*,\s*(?=\d)'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following string: ABC . I would like to capture the following groups
Consider following string Some string with quotes and \pre-slashed\ quotes Using regex, I want
Consider the following String : 5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+ Here is how I want to split string,
Consider the following string '35=_-235-b-35=35-35=2-135=a-35=123-235=2-35=a-53=1-53=a-553=b' I'd like to extract everything that matches 35= followed
Consider this string, var string = "sometext/#a=some_text/#b=25/moretext"; What I would like to do is
Consider a string that looks like this: RR1 S5 C92 This a rural route
Consider a string array shaped like this: string[] someName = new string[] { First,
Consider: Dim line As String Using readFile As New StreamReader(SalesUpdateFile) While (line = readFile.ReadLine)
Consider string.Format() whose parameters are a string and, among others in the overload list,
Consider a string like this, type=xxx.yyy.zzz, xxx.yyy, Version=2.0.3.0, Culture=neutral, PublicKeyToken=null I read a resx

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.