if you know for sure that there are only going to be 2 places where you have a list of digits in your string and that is the only thing you are going to pull out then you should be able to simply use

\d+
Answer from Seattle Leonard on Stack Overflow
Discussions

How to extract number from string c# - Stack Overflow
Well, a simply regular expression ... etc in strings? var matches = Regex.Matches("transform(23,45)", "([0-9]+)"); foreach (Match match in matches) { int value = int.Parse(match.Groups[1].Value); // Do work. } ... +1 for "periods, commas, etc...", and to add to the list: non-10-base number literals, ... More on stackoverflow.com
🌐 stackoverflow.com
Regex extract number from string - Stack Overflow
I need to extract the highlighted number from these two strings. Dear IBBL CH Purchased 10.00 BDT at grameenphone.com, BD on 05.09.21 20:06 Card ***9793 Avl Bal: 930.53 BDT Help:16259 Get 10%disc... More on stackoverflow.com
🌐 stackoverflow.com
c - Extracting numbers from the string using regex - Stack Overflow
I am trying to extract the number 4 and 3 from the string /ab/cd__my__sep__4__some__sep__3. I am trying with regex but not sure how would I do this. I wrote the following code, but it just prints out More on stackoverflow.com
🌐 stackoverflow.com
C++ Extract number from the middle of a string - Stack Overflow
If they're supposed to be in that ... allowing strings of a different format would be an error. 2015-05-07T21:34:03.233Z+00:00 ... If you would like an explanation of the regex syntax I would be happy to provide such an explanation. 2015-05-09T02:10:38.45Z+00:00 ... @matthew the original title seems to me IMO to capture the intent of the question ... I.e. the question to me seems more to be "generally how would you extract a number from a ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
University Innovation Hub
gws.sandbox.iam.s.uw.edu › home › smartsheet
Extracting Numbers: 5 Regex Tricks - University Innovation Hub
June 25, 2025 - Character classes in regex allow you to match any one of a set of characters. This is particularly useful when you need to extract numbers from strings containing various delimiters or separators.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-extract-numbers-from-a-string-using-regular-expressions
How to extract numbers from a string using regular expressions?
November 21, 2019 - Enter sample text: this is a sample 23 text 46 with 11223 numbers in it Digits in the given string are: 23 46 11223 · import java.util.regex.Matcher; import java.util.regex.Pattern; public class Just { public static void main(String[] args) { String data = "abc12def334hjdsk7438dbds3y388"; //Regular expression to digits String regex = "([0-9]+)"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Creating a Matcher object Matcher matcher = pattern.matcher(data); System.out.println("Digits in the given string are: "); while(matcher.find()) { System.out.print(matcher.group()+" "); } } }
Find elsewhere
Top answer
1 of 3
3

match[0] refers to the part of the text matched by the entire pattern. match[1] is the match corresponding to the first capture (parenthesized subpattern).

Note that &s[match[1].rm_so] gives you a pointer to the start of the capture, but if you print the string at that point, you will get the part of the string starting at the beginning of the capture. In this case, that doesn't really matter. Since you're using sscanf to extract the integer value of the captured text, the fact that the substring isn't terminated immediately doesn't matter; it's not going to be followed by a digit, and sscanf will stop at the first non-digit.

But in the general case, it's possible that it will not be so easy to identify the end of the matched capture, and you can use one of these techniques:

If you want to print the capture, you can use a computed string width format: (See Note 1.)

printf("%.*s\n", match[1].rm_eo - match[1].rm_so, &s[match[1].rm_so]);

If you have strndup, you can easily create a dynamically-allocated copy of the capture: (See Note 2.)

char* capture = strndup(&s[match[1].rm_so], match[1].rm_eo - match[1].rm_so);

As a quick-and-dirty hack, it is also possible to just insert a NUL terminator (assuming that the searched string is not immutable, which means that it cannot be a string literal). You'll probably want to save the old value of the following character so that you can restore the string to it's original state:

char* capture = &s[match[1].rm_so];
char* rest = &s[match[1].rm_eo];
char saved_char = *rest;
*rest = 0;
/* capture now points to a NUL-terminated string. */
/* ... */
/* restore s */
*rest = saved_char;

None of the above is really necessary in the context of the original question, since the sscanf as written will work perfectly if you change the start of the string to scan from match[0] to match[1].

Notes:

  1. In the general case, you should test to make sure that a capture was actually found before trying to use its offset. The rm_so member will be -1 if the capture was not found during the regex search That doesn't necessarily mean that the search failed, because the capture could be part of an alternative not used in the match.

  2. Don't forget to free the copy when you no longer need it. If you don't have strndup, it's pretty easy to implement. But watch out for the corner cases.

2 of 3
0

Since you are using sscanf(), there is no need to use a regex. You can parse the two numbers from your string using sscanf() alone using the format string: "%*[^0-9]%d%*[^0-9]%d" where "%*[^0-9]" uses the assignment suppression '*' to read and discard all non-digit characters and then uses "%d" to extract the integer value. The full format-string just repeats those two patterns twice.

A short example using your input could be:

#include <stdio.h>

int main (void) {

    char *s = "/ab/cd__my__sep__4__some__sep__3";
    int a, b;

    if (sscanf (s, "%*[^0-9]%d%*[^0-9]%d", &a, &b) == 2)
        printf ("a: %d\nb: %d\n", a, b);
    else {
        fputs ("error: parse of integers failed.\n", stderr);
        return 1;
    }
}

Example Use/Output

$ ./bin/parse2ints
a: 4
b: 3

If you find yourself attempting to parse something that sscanf() cannot handle, then a regex is appropriate. Here, sscanf() is more than capable of handling your needs alone.

🌐
Tcl Wiki
wiki.tcl-lang.org › page › Extract+Numbers+From+a+String
Extract Numbers From a String
WJG 2022-10-03 PYK 2022-10-09: ... would append a either a comma or full-stop as sentence punctuation, these are removed from any result. proc extractNumbers str { set buff "" set res "" set lc "" set pf "-+" ;# number sequence prefixes set if ".,/ ^" ;# number sequence ...
🌐
Quora
quora.com › Can-someone-provide-an-example-of-how-to-use-regular-expressions-to-extract-numbers-or-words-from-a-given-string-in-the-C-programming-language
Can someone provide an example of how to use regular expressions to extract numbers or words from a given string in the C programming language? - Quora
Answer: “Can someone provide an example of how to use regular expressions to extract numbers or words from a given string in the C programming language?” C doesn’t have any built-in regular expression primitives so you’ll need to use an external library such as glibc [1]or PCRE [2]. ...
🌐
TiddlyWiki
talk.tiddlywiki.org › t › need-a-regexp-to-extract-the-number-from-a-string › 10355
Need a regexp to extract the number from a string - Talk TW
August 6, 2024 - I would like for a regexp that extracts the number from an arbitrary string. The string can have chars before or after the number, but we can assume there is only one segment of digits forming the number. arbitrarystrin…
🌐
Octoparse
octoparse.com › blog › regex-how-to-extract-all-phone-numbers-from-strings
RegEx: How to Extract All Phone Numbers from Strings | Octoparse
July 10, 2022 - This is a fast guide for beginners to use regular expressions to extract phone numbers from strings. RegEx stands for Regular Expression, which is an object that describes the pattern of a string. With this expression understandable to the computer, we are able to locate the data that matches this pattern and retrieve the information we want.
🌐
UiPath Community
forum.uipath.com › help › studio
Please help me with a RegEx to extract only the numbers from a string variable - Studio - UiPath Community Forum
March 12, 2024 - Hi, I have a string variable which contains both text and numbers assigned to a string variable. I want to extract the numbers from the string using RegEx. Example: If the text is “Text#$1234”, I want to extract “1234” …