Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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.

'''
Comment

PREVIOUS NEXT
Code Example
Python :: soap 1.2 request python 
Python :: python print value and variable name 
Python :: bold some letters of string in python 
Python :: python array input from user 
Python :: how to install tkinter in pycharm 
Python :: subtract number from each element in list python 
Python :: how to convert dataframe to text 
Python :: index a dictionary python 
Python :: python json web request 
Python :: python fill zeros left 
Python :: Triangle Quest 
Python :: Cast image to float32 
Python :: print python float precision 
Python :: convert .py to .exe 
Python :: default orange and blue matplotlib 
Python :: open excel through python 
Python :: creating a sqlite3 table in python and inserting data in it 
Python :: python see if a number is greater than other 
Python :: Support Vector Machine (SVM) classifier 
Python :: python list comprehension 
Python :: python split string to sentences 
Python :: bytearray to hex python 
Python :: round tuple 
Python :: python defaultdict(list) 
Python :: spacy tokineze stream 
Python :: python comment 
Python :: how to capture cmd output in python 
Python :: planets code 
Python :: pandas head sort by colun name 
Python :: train slipt sklearn 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =