-2

请原谅我再次发布此内容,但我认为我真的搞砸了我以前的帖子。因为评论区只允许这么多字符,我无法更好地解释自己,而且我没有看到回复的选择,以便我有更多的空间。所以如果没有人介意,让我试着解释我需要的一切。基本上我需要从以下位置翻转具有前缀或后缀“L”或“R”的 3D 对象的名称:

1:“L”与“R”,2:“R”与“L”,或3:不改变。

这是用于 Maya 中的脚本,以便复制选定的对象并在那里翻转名称。我将复制部分打包,现在它是关于尝试根据 5 种可能性翻转复制对象的名称。从前 2 个前缀开始,复制的对象需要以任何一个开头

"L_" or "R_", match case doesn't matter.

接下来的 2 个后缀必须是:

"_L" or "_R" with a possible extra character "_", such as "Finger_L_001".

现在在这个论坛上搜索,我想找到了几乎我正在寻找的东西。我复制了语法并将用户的搜索字符替换为我的“L_”和“ L”,只是为了看看它是否会起作用,但只有一些期望。由于我只知道正则表达式的基础知识,比如“L. *”会找到L_Finger_001,所以我真的不明白下面这行语法以及为什么第二选择不留下它作为L_Finger。所以也许这不是我需要的,或者是吗?有人可以解释一下吗?我尝试搜索 (?P) 和 (?P\S+) 等关键字,但没有找到任何内容。因此,无需进一步说明,这里是语法....

>>> x = re.sub(r'(?P<prefix>_L)(?P<key>\S+)(?(prefix)|L_)','\g<key>',"L_Finger")
>>> x
'L_Finger'
>>> x = re.sub(r'(?P<prefix>L_)?(?P<key>\S+)(?(prefix)|_L)','\g<key>',"L_anything")
>>> x
'Finger'
#

更新 11\10\13 下午 3:52 ET 好的,所以我稍微调整了代码,但我喜欢它的发展方向。实际上,我最初的想法是使用字典,但我可以弄清楚如何搜索。通过 kobejohn 引导我朝着正确的方向定义所有可能性,这开始变得有意义。这是一个在制品

samples = ('L_Arm',
           'R_Arm',
           'Arm_L',
           'Arm_R',
           'IndexFinger_L_001',
           'IndexFinger_R_001',
           '_LArm')

prefix_l, prefix_r = 'L_', 'R_'
suffix_l, suffix_lIndex, suffix_r, suffix_rIndex = '_L', '_L_', '_R', '_R_'

prefix_replace = {prefix_l: prefix_r, prefix_r: prefix_l}
suffix_replace = {suffix_l: suffix_r, suffix_r: suffix_l}
suffixIndex_replace = {suffix_lIndex: suffix_rIndex, suffix_rIndex: suffix_lIndex}

results = dict()
for sample in samples:
    # Default value is no modification - may be replaced below
    results[sample] = sample

    # Handle prefixes
    prefix = prefix_replace.get(sample[:2].upper())
    if prefix :
        result = prefix+sample[:2]

    else :
        #handle the suffixes
        suffix_partition = sample.rpartition("_")
        result = suffix_partition[0] if suffix_partition[2].isdigit() else sample
        suffix = suffix_replace.get(result[-2:])
        print("Before: %s --> After: %s"%(sample, suffix))
4

2 回答 2

0

Ok, I guess multiple regular expressions is valid too. Here is a way using re's similar to the ones you found. It assumes real prefixes (nothing before the prefix) and pseudo-suffixes (anywhere except the first characters). Below that is a parsing solution with the same assumptions.

import re

samples = ('L_Arm',
           'R_Arm',
           'Arm_L',
           'Arm_R',
           'IndexFinger_L_001',
           'IndexFinger_R_001',
           '_LArm')
re_with_subs = ((r'(?P<prefix>L_)(?P<poststring>\S+)',
                 r'R_\g<poststring>'),
                (r'(?P<prefix>R_)(?P<poststring>\S+)',
                 r'L_\g<poststring>'),
                (r'(?P<prestring>\S+)(?P<suffix>_L)(?P<poststring>\S*)',
                 r'\g<prestring>_R\g<poststring>'),
                (r'(?P<prestring>\S+)(?P<suffix>_R)(?P<poststring>\S*)',
                 r'\g<prestring>_L\g<poststring>'))
results_re = dict()
for sample in samples:
    # Default value is no modification - may be replaced below
    results_re[sample] = sample
    for pattern, substitution in re_with_subs:
        result = re.sub(pattern, substitution, sample)
        if result != sample:
            results_re[sample] = result
            break  # only allow one substitution per string

for original, result in results_re.items():
    print('{0} --> {1}'.format(original, result))

Here is the parsing solution.

samples = ('L_Arm',
           'R_Arm',
           'Arm_L',
           'Arm_R',
           'IndexFinger_L_001',
           'IndexFinger_R_001',
           '_LArm')

prefix_l, prefix_r = 'L_', 'R_'
suffix_l, suffix_r = '_L', '_R'
prefix_replacement = {prefix_l: prefix_r,
                      prefix_r: prefix_l}
suffix_replacement = {suffix_l: suffix_r,
                      suffix_r: suffix_l}
results = dict()
for sample in samples:
    # Default value is no modification - may be replaced below
    results[sample] = sample
    # Handle prefixes
    prefix = sample[:2].upper()
    try:
        results[sample] = prefix_replacement[prefix] + sample[2:]
        continue  # assume no suffixes if a prefix found
    except KeyError:
        pass  # no valid prefix
    # Handle pseudo-suffixes
    start = None
    for valid_suffix in (suffix_l, suffix_r):
        try:
            start = sample.upper().rindex(valid_suffix, 1)
            break  # stop if valid suffix found
        except ValueError:
            pass
    if not start is None:
        suffix = sample[start: start + 2].upper()
        new_suffix = suffix_replacement[suffix]
        results[sample] = sample[:start] + new_suffix + sample[start + 2:]

for original, result in results.items():
    print('{0} --> {1}'.format(original, result))

gives the result:

  • L_Arm --> R_Arm
  • R_Arm --> L_Arm
  • IndexFinger_L_001 --> IndexFinger_R_001
  • Arm_L --> Arm_R
  • _LArm --> _LArm
  • IndexFinger_R_001 --> IndexFinger_L_001
  • Arm_R --> Arm_L
于 2013-11-10T18:51:24.080 回答
0

你可以通过这个棘手的正则表达式模式来做到这一点>>

if re.search('(^[LR]_|_[LR](_|$))', str):
    str = re.sub(r'(^[LR](?=_)|(?<=_)[LR](?=(?:_|...$)))(.*)(?=.*\1(.))...$',
                 r'\3\2', str+"LRL")

请参阅此演示


或者,您可以通过一行代码来完成 >>

str = re.sub(r'(^[LR](?=_)|(?<=_)[LR](?=(?:_|...$)))(.*)(?=.*\1(.))...$', r'\3\2', str+"LRL")[:len(str)]

请参阅此演示

于 2013-11-10T19:26:55.657 回答