Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

regex in python

# Let's say you want to check for a phone number in a string
# Note: Remove indentation

	import re
	phone_num_regex = re.compile(r'ddd-ddd-dddd')
	mobile_string = 'My number is 415-555-4242' # Not real number
	any_phone_numbers = phone_num_regex.search(mobile_string)
	print(any_phone_numbers)

The r in front of the string means it's a raw string (/n, /t, etc doesn't work)
In regex, if we use d, it will look for any digit in your string (0-9)

If we search for ddd-ddd-dddd, it will look for anywhere in the
string where there is a digit, followed by a digit, followed by a digit, followed
by a hyphen, ...

You can also use it in an if statement to check if there is a match or not
between a regex and a string with 're.match(regex, string)'


Source by www.py4e.com #
 
PREVIOUS NEXT
Tagged: #regex #python
ADD COMMENT
Topic
Name
1+9 =