Regex py

From DreamsteepWiki

Jump to: navigation, search

SIMPLE MATCHING

import re

a = 'abc'
b = 'a'

#re.compile
regex_obj = re.compile(('^.*'+b ))
print regex_obj.findall(a)


import re

"""
MEL-PYTHON REGEX EXAMPLES

"""

###############################################
"""
match "[0-9]+" "sceneRender019.iff";
// Result: 019 //

"""
#python
#regex_obj = re.compile("[0-9]+")

###############################################
"""
//20" fir   match everything 0-9  to the left of " symbol
match "[0-9]+(\")" "20\" fir"
 // Result: 20" //
 
"""
#regex_obj = re.compile('\"')

###############################################

"""
// extractFileName "aa/bb/cc.poo";
// Result: cc //
"[^/\\]*$"
"""

regex_obj = re.compile("[/]")


#group()  	Return the string matched by the RE
#start() 	Return the starting position of the match
#end() 	Return the ending position of the match
#span() 	Return a tuple containing the (start, end) positions of the match

###############################################
#match an excaped " char
#regex_obj = re.compile("\"")

##############

#THESE ARE THE SAME OPERATION ,#match a character to the left of escaped "
#regex_obj = re.compile(".\"")    #(is really  '."'
#regex_obj = re.compile('."')




###############################################
"""
#this appears to spilt by all non a-z chars (with .findall)

regex_obj = re.compile("[a-z]")
"aa/bb/cc.poo"
result: ['a','a','b','b','c','c','p','o','o']

######################
regex_obj = re.compile("[a-z]*")
"aa/bb/cc.poo"
result: ['aa','','bb','','cc','','poo','']


"""



###############################################
#crop_leftmost
#regex_obj = re.compile("^[^|]*")

#crop_rightmost
#regex_obj = re.compile("^[^|]*")

#crop_lefttmost
#regex_obj = re.compile("[^|]*$")

###############################################


#.match("ba", 1)           # succeeds

mtchstr = "aa|bb|cc.poo"


if regex_obj.findall(mtchstr):
 print 'yes'
 print regex_obj.findall(mtchstr)
 
else:
 print 'no'
 
#match "[0-9]+" "sceneRender019.iff";


#re.compile()
Personal tools