regex pattern in order to get a list of numbers from a string
-
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
-
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
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
-
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
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.