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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:19:24+00:00 2026-05-20T06:19:24+00:00

strings = [1 asdf 2, 25etrth, 2234342 awefiasd] #and so on Which is the

  • 0
strings = ["1 asdf 2", "25etrth", "2234342 awefiasd"] #and so on

Which is the easiest way to get [1, 25, 2234342]?

How can this be done without a regex module or expression like (^[0-9]+)?

  • 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-20T06:19:24+00:00Added an answer on May 20, 2026 at 6:19 am

    One could write a helper function to extract the prefix:

    def numeric_prefix(s):
        n = 0
        for c in s:
            if not c.isdigit():
                return n
            else:
                n = n * 10 + int(c)
        return n
    

    Example usage:

    >>> strings = ["1asdf", "25etrth", "2234342 awefiasd"]
    >>> [numeric_prefix(s) for s in strings]
    [1, 25, 2234342]
    

    Note that this will produce correct output (zero) when the input string does not have a numeric prefix (as in the case of empty string).

    Working from Mikel’s solution, one could write a more concise definition of numeric_prefix:

    import itertools
    
    def numeric_prefix(s):
        n = ''.join(itertools.takewhile(lambda c: c.isdigit(), s))
        return int(n) if n else 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Strings are considered reference types yet can act like values. When shallow copying something
i need a Regex to do this ( catch only real words ): Inputstring:
I have a method like this: def aMethod(param: String = asdf) = { ...
I have a regex that I'm trying use to validate against strings. Trying to
Here are two reasons to think strings are objects. First, you can create a
This is valid code: void func(IEnumerable<string> strings){ foreach(string s in strings){ Console.WriteLine(s); } }
I have an assembly asdf.dll and it has a class 'Class1'. How can I
I don't understand why this code behaves differently in different implementations: (format t asdf)
str = abc xyz asdf @ Rs1.5/day.poi qwerty. I want to use regex in
This works on dev: <connectionString name=asdf value=Data Source=.\;Initial Catalog=fmwebapp1;Integrated Security=True /> <connectionType value=System.Data.SqlClient.SqlConnection, System.Data,

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.