regex will be your friend here. Dont bother trying to learn all of regex,
but knowing some basic patterns are very handy for exactly this situation
(and figure out more complicated ones if/when you need to). For instance,
to match a number followed by a single capital letter you could use a
pattern like:

"([0-9][A-Z])"

The [0-9] is literally "match any digit", and the [A-Z] means match any
capital letter right after it. If you want to include lower case letters
then you could use [A-Za-z]. Wrapping the whole thing in brackets "()"
means you get a "group" containing the match, which you can use to
find/replace later.

some working code to play with:

import re
item = 'l_primCovFeathers1AEnd_loc'
pattern = "([0-9][A-Z])"
match = re.search(pattern, item)
if match:
    letter_number = match.group(0)
    number_letter = letter_number[::-1]
    new_item = item.replace(letter_number, number_letter)
    print new_item
else:
    print "NO MATCH: %s" % item

Hope that's useful! In terms of regex worth *knowing* offhand, I'd
recommend learning what the basic characters are and how to use them i.e.
[] {} () ^ $ . + *    .... They get me by in 99% of situations.

Phil

On Mon, 4 Jan 2016 at 11:12 Rudi Hammad <[email protected]> wrote:

> hello,
> first of all, happy new year!!!
> this is my first 2016 question. I have a list like myList=[
> 'l_pollexFeatherWingA_loc', 'l_primCovFeathers1AEnd_loc',
> 'l_primCovFeathers1A_loc', 'l_primCovFeathers2AEnd_loc']
> actually, the list is a lot longer but that will work. I want to swap "1A"
> and to "2A" , to "A1" and "A2". The first element
> "l_pollexFeatherWingA_loc" is fine because it doesn´t have a number before.
> I tried using split, but I can´t figure it out, because the elements of
> the list are too different. Just swapping "number+Letter" to
> "letter+number" would be perfect, but I guess it is not possible.
> I search on google, but the solutions I found didn´t work for me.
> cheers!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/2d95fadb-d4a4-4565-8510-60092ded3c6d%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/2d95fadb-d4a4-4565-8510-60092ded3c6d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPzKG6X1%3D%3D0Ea8CyU9RTNeZtZWdqek9epSHXhrEgLy6Aeqk_ew%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to