Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

python find digits in string

import re

some_string = "Here is some text. We want to find matches in. Let's find the number 1234 and the number 5342 but not the number 942003. "

regex_pattern = re.compile(r"D(d{4})D")
matching_numbers = re.findall(regex_pattern, some_string)

print(matching_numbers)

'''
returns: 1234, 5342
'''

'''

The important part here is the `regex pattern`. Let's break it apart into 3 sections:

`r"<first><desired_match><last>"`

1. <first> is `D`
	-> tells python that anything that isn't a digit, just ignore it.

2. <desired_match> is `(d{4})`
	-> says that the pattern in `()` should be considered as a match. 

3. <last> is `D`
	-> tells python that anything that isn't a digit, just ignore it.


* Putting `d` would yeild all 3 sets of numbers `1234`, `5342`, and `942003`. 
* `d{4}` is saying that the digits must be a at least 4 digits to match. 
* The `<last>` portion prevents digits larger than 4.

'''
Source by github.com #
 
PREVIOUS NEXT
Tagged: #python #find #digits #string
ADD COMMENT
Topic
Name
4+3 =