To match and capture (= extract) the number, you can use a regular-expression.
TL;DR: I would recommend re.match(r'NC(\d+)', s).group(1) (details in the last section).
Regex to match a number
To match a number with a minimum length of 1 digit, use the regular-expression (patter) \d+' for one or many digits, optionally inside a capturing-group as (\d+)` where:
\d is a character class (meta-character) for digits (of range 0-9)
+ is a quantifier matching if at least one occurrence of preceding pattern was found
( and ) form a capturing-group of the enclosed sub-regex
Test your regex on regex101 or regexplanet and choose the right flavor/language/engine (here: Python).
In Python use the built-in regex module re. Define the regex as raw-string like r'\d+'.
Find to extract only the number or empty list
Either function re.findall to find a list of occurrences:
import re
s = 'NC123456Sarah Von Winkle'
pattern = r'\d+'
occurrences = re.findall(pattern, s)
print(occurrences)
Prints:
['123456']
The first number occurrences[0] is yours if not empty:
if len(occurrences) == 0:
print('no number found in: ' + s)
else:
number = occurrences[0]
Split to get all parts
Or function re.split to split the string into parts:
import re
s = 'NC123456Sarah Von Winkle'
pattern = r'(\d+)'
parts = re.split(pattern, s)
print(parts)
Prints:
['NC', '123456', 'Sarah Von Winkle']
Note: without the capture-group (i.e. without parentheses ()) the output would be just: ['NC', 'Sarah Von Winkle'] (excluding the splitter-pattern)
Here you would get the number in second part parts[1] as long as non-number-prefix like "NC" is guaranteed and followed by a number.
Extract with a capturing-group
Use the group function together with a regex containing a capturing-group:
import re
s = 'NC123456Sarah Von Winkle'
capture_number_pattern = re.compile(r'NC(\d+)')
extracted = capture_number_pattern.match(s).group(1)
print(extracted)
Prints:
123456
Note: re.compile returns a compiled pattern. This can optimize performance when pattern is re-used multiple times and improve readability of the code.
Pay attention: To make your matching robust and defensive test if there is a match, otherwise an error is raised at runtime, see Python shell:
>>> extracted = capture_number_pattern.match('NCHelloWorld2022').group(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
You can test if a match was found or fail-fast if match is None:
s = 'NCHelloWorld2022'
match = capture_number_pattern.match(s)
if not match:
print('No number found in:' + s)
else:
print(match.group(1))
prints:
No number found in:NC123456Sarah Von Winkle