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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:06:08+00:00 2026-06-03T23:06:08+00:00

Say I have a data string that can have formatting that varies. Nominally the

  • 0

Say I have a data string that can have formatting that varies. Nominally the data in the string would be seperated by spaces but that’s not always the case, so a simple .split(' ') won’t work in this instance.

An example string is:

string = '2012 05 06 04:20:00.0500 FOOBAR 4.7E+10 -55 33.0 555~2767 B 12 \r\n'

To get all the numbers I need, which can contain exponents, start with -, +, or ~, or not have a space separating them I can use:

re.findall(r'[~+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?', string)
# giving the result;
['2012', '05', '06', '04', '20', '00.0500', '4.7E+10', '-55', '33.0', '555', '~2767', '12']

I also need just the single character (in this case B) from the string. This single character can be either B,F, or O and I can get this and avoid the FOOBAR in my string by using:

re.findall(r'((?:(?:\b))[FBO]\b)', string)
# giving the result:
['B']

But what I need is to get a result that combines the two results above. I could always append the list with the second result, but I would really like the position of the results in the corresponding list to appear in the order in which they exist in the original string string. That is, I want a list that looks like:

['2012', '05', '06', '04', '20', '00.0500', '4.7E+10', '-55', '33.0', '555', '~2767', 'B', '12']   

Any ideas? Or is there a better way?

  • 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-03T23:06:10+00:00Added an answer on June 3, 2026 at 11:06 pm

    How about:

    re.findall(r'([~+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|(?:(?:\b))[FBO]\b)', str)
    

    This returns:

    ['2012', '05', '06', '04', '20', '00.0500', '4.7E+10', '-55', '33.0', '555', '~2767', 'B', '12']
    

    Also, not to nag, but overwriting the Python type str with a variable name made be shudder for a second there.

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

Sidebar

Related Questions

Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
I have a string say string s =C:\\Data , I have an array which
Say I have a data file that I want to process; I want to
I have a JSON character string that I put into a data frame. I
Let's say I have a string contaning HTML markup with many img tags that
I have another SQL/access 2007 question that seems really basic but I'm not sure
Let's say I have a Car class in a game that can be played
Say I have the following ember-data model: App.Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'),
Say if I have an initial string that could contain either an integer or
Say I have a database that stores table data by year using an identical

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.