>>> import re
 >>> reg = re.compile( "([^.]+)\.\w+\[(\d+)\]$" )
 >>> reg.match( 'parent|foo_barSpangle1.f[22]' ).groups()
('parent|foo_barSpangle1', '22')
 >>> reg.match( 'cubeThing.vtx[0]' ).groups()
('cubeThing', '0')

so to break it down:

([^.]+)   one or more of anything that's not a period. this will  
actually allow more than what a proper node name can have, but i'm  
assuming you'll be passing proper nodes.  the parentheses tells re  
that we want to capture the result

\.  a period

\w+  one or more letters.  you could wrap parentheses around this part  
if you also want to know what type of component you're dealing with:  
f, vtx, e

\[(\d+)\]  one or more numbers surrounded by brackets .  capture the  
numbers

$   the end of the regex must match all the way through to the right  
end of the input string


-chad







On Mar 30, 2009, at 12:33 PM, Tucker wrote:

>
> The problem is, the user selects a face. But i need both the face
> number and object to drive two different functions.
> For example -
> pCube1.f[4] I'd need the number '4' and the object 'pCube1'.
>
> Therefore i thought i needed to tokenize the user selection string to
> get both the face number and the object name. I did this by -
>
> # User selects face. Get user selected Face
> selected = om.MSelectionList()
> om.MGlobal.getActiveSelectionList(selected)
> selectedObj = []
> selected.getSelectionStrings(selectedObj)
>
> # Tokenize for the correct face number
> faceNumString = selectedObj
>
> # Tokenize String
> tokenFace = re.findall('\[(\d+:*\d*)\]$', faceNumString)[0]
> print tokenFace
>
> I understand that the findall command is not reading the variable but
> reading it as it's own stand alone characters. I was wondering if
> there was any way to tokenize the variable so i get both the face
> number and the object name? Any help any one can give would be greatly
> appreciated.
>
> >


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to