Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. regex pattern in order to get a list of numbers from a string

regex pattern in order to get a list of numbers from a string

Scheduled Pinned Locked Moved Regular Expressions
regex
3 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rafael Soteldo
    wrote on last edited by
    #1

    Hi there:

    Suppose I have a string with “1: a string, 2: other 5 string, 3: something 8; else”, what would be the regex pattern to obtain “1,2,3” as a result

    Thank you in advance,

    Rafael

    P A 2 Replies Last reply
    0
    • R Rafael Soteldo

      Hi there:

      Suppose I have a string with “1: a string, 2: other 5 string, 3: something 8; else”, what would be the regex pattern to obtain “1,2,3” as a result

      Thank you in advance,

      Rafael

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      Non-capturing groups are your friend. They look like (?: ... ) What you want is something like [0-9]+(?::) to pick off one or more digits followed by a colon, but the colon is excluded from the result string. Note to self: Don't answer tech questions before morning coffee!

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      1 Reply Last reply
      0
      • R Rafael Soteldo

        Hi there:

        Suppose I have a string with “1: a string, 2: other 5 string, 3: something 8; else”, what would be the regex pattern to obtain “1,2,3” as a result

        Thank you in advance,

        Rafael

        A Offline
        A Offline
        Aghast nj
        wrote on last edited by
        #3

        You don't say what environment you are working in, so it's hard to give you useful code. However, you can probably get what you want by either doing a search & replace operation, possibly repeated, or by using some kind of "regex iterator" approach. For C++, it would be a regex iterator (iterate over all the matches of THIS pattern in THAT string). For something like Python it would be find all occurrences of THIS pattern in THAT string. In order to drop the "8;" from your example sentence, you'd want to provide a better pattern than just "some digits." Instead, use something like /(\d+):/ to match on one-or-more digits followed by a colon. You want to capture the digits, not the colon. If you can just iterate over all matches in the string, that will probably be enough. If you're doing sed or some other editor, you'll have to search and replace the line. If possible, see if you can do something like a non-greedy match (.*?) to match all the text between matches, and then replace it with a comma. (FYI: The ? operator is a non-greedy modifier in Perl-style regex engines. Other regex engines may not support this at all, or they may have a different syntax-- for example, Vim would use .\{-} for that.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups