【问题标题】:concat string with result of regular expression python用正则表达式python的结果连接字符串
【发布时间】:2012-05-26 15:36:40
【问题描述】:

maya ascii 文件包含如下指令行。

...
createNode transform -n "pCylinder1";
createNode mesh -n "pCylinderShape1" -p "pCylinder1";
    setAttr ".vif" yes;
    setAttr ".uvst[0].uvsn" -type "string" "map1";
createNode transform -n "pPlane1";
    setAttr ".t" -type "double3" 7.3666236108700112 0 -4.2288466031573595 ;
createNode mesh -n "pPlaneShape1" -p "pPlane1";
    setAttr ".uvst[0].uvsn" -type "string" "map1";
    setAttr ".cuvs" -type "string" "map1";
createNode transform -n "pTorus1";
    setAttr ".t" -type "double3" -0.47688973199150198 0 -10.843417358550912 ;
...
connectAttr "polySphere1.out" "pSphereShape1.i";
connectAttr "polyCube1.out" "pCubeShape1.i";
connectAttr "polyCylinder1.out" "pCylinderShape1.i";
connectAttr "polyPlane1.out" "pPlaneShape1.i";
connectAttr "polyTorus1.out" "pTorusShape1.i";

...

在这些行中,我需要搜索一个看起来像任何行的行 以下几行。

createNode transform -n nodeName -p "FG";
createNode transform -n nodeName -p "BG";
createNode transform -n nodeName -p "MG";

我应该使用什么正则表达式来查找上述任何内容。

【问题讨论】:

  • 你需要在这个问题上付出更多的努力。现在几乎无法理解。
  • 好的。我需要将搜索结果(可能是 FG|BG|MG )与已知字符串连接起来。得到它,现在我需要搜索这个字符串。在上面的例子中 line2 是已知的。 zam 是未知的,即。行可以包含 FG、BG 或 MG。 say line = "createNode transform -n \"water\" -p \"FG\";"
  • 尝试显示示例输入和相应的预期输出。
  • line = "createNode 变换 -n \"water\" -p \"FG\";"

标签: python regex


【解决方案1】:

这是我对您的描述所能做的最好的事情:

我需要将搜索结果(可能是 FG|BG|MG )与已知字符串连接

import re

line = "createNode transform -n \"water\" -p \"FG\";" 

m = search(r'(FG|BG|MG)',line)
if m:
    result = m.groups()[0]

    # What do you want to concat it to? 
    known_string = "known_string" + result

"现在我需要搜索这个字符串。 为什么?你已经搜索过了。
"zam 未知 - 不,不是,它是re.compile()的结果

抱歉,我无法进一步了解您的描述

【讨论】:

  • 输入 --> line = "createNode transform -n \"water\" -p \"MG\";" zam = re.compile(r'(\" -p \")(FG|MG|BG)(\")') if re.search(zam,line): print 'found'
  • 我很抱歉不能说清楚。这就是我需要的。谢谢
猜你喜欢
  • 2017-12-17
  • 1970-01-01
  • 2011-11-28
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 2020-03-05
相关资源
最近更新 更多