我有一个string
格式的"12345-0012-0123"
,我想把它们都改成12345-0012-123"
格式,这样破折号后面的最后一部分就只有三位数而不是四位数了。你知道吗
在所有情况下,破折号后面的最后一部分最多只有三个实数,我需要在0001、0012、0123前面加一个零。。。你知道吗
一些字符串,我将编辑已经在正确的格式,所以快速检查,看看我甚至需要执行更正将更好。。。你知道吗
编辑:已解决!!你知道吗
对于任何一个感兴趣的人来说,这是我正在使用的ArcGIS计算器代码,它是由anirudh提供的答案修改而来的。。。你知道吗
#Convert to three digit count
def FixCount(s):
length = len(s[s.rfind('-')+1:])
if length > 3:
return s.rstrip(s[s.rfind('-')+1:])+s[s.rfind('-')+2:]
else:
return s.rstrip(s[s.rfind('-')+1:])+s[s.rfind('-')+1:]
__esri_field_calculator_splitter__
FixCount(str( !input_field_id! ))
这是regular expressions的工作!你知道吗
给出:
我们要匹配两组:
+
)数字(d
),后跟一个-
,后跟一个或多个(+
)数字(d
),后跟一个-
+
)0
,我们没有捕获(没有()
)。如果只想匹配单个0
,请删除+
!你知道吗+
)数字(d
)然后我们要替换(^{} )我们的字符串
s
,它将这个正则表达式与那些capture groups中的内容匹配。你知道吗注意:
^{} 返回修改后的
s
,它不会就地修改它。你知道吗这不一定是正则表达式的工作!你知道吗
示例用法:
因此,在这里给出一些其他答案:
正则表达式在这里是过度杀伤力的,而且与仅仅使用内置表达式相比速度很慢。你知道吗
你可以像johnsyweb所说的那样使用regex,如果你不想使用regex,也可以使用下面的。你知道吗
相关问题 更多 >
编程相关推荐