Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

regular expressions

import re

message = "123-456-7890 is a phone number and so is 111-111-1111"

# designate the phone number has one group
PhoneNumRegEx = re.compile(r"ddd-ddd-dddd")

#find the first phone number
matched_object = (PhoneNumRegEx.search(message))

#prints out the whole number. Can't put anything in the parenthesis
print(matched_object.group())




# designate the phone number has two groups
PhoneNumRegEx = re.compile(r"(ddd)-(ddd-dddd)")
matched_object = (PhoneNumRegEx.search(message))
#prints out the numbers withi the first parenthesis. 
print(matched_object.group(1))

#gets all the phone numbers in the message
print(PhoneNumRegEx.findall(message))


#if looking for a parenthesis, put  before each parenthesis
PhoneNumRegEx = re.compile(r'(ddd) ddd-dddd')
message_area_code = "dsdfs (111) 444-3333 gfgg"
matched_object = (PhoneNumRegEx.search(message_area_code))
print(matched_object.group())

#find the following words that start with uni
prefix = re.compile(r'uni(corn|cycle|verse)')
mo = prefix.search("In what universe does a unicorn ride a unicycle?")

#only returns whole word if you use search, 
#if you use findall, and print(mo), only sufix is printed
print(mo.group())

Source by gis.stackexchange.com #
 
PREVIOUS NEXT
Tagged: #regular #expressions
ADD COMMENT
Topic
Name
2+3 =