[英]split string after 2nd character occurence
This is my string 70354871699YG000_7798419T0000_1
.这是我的字符串
70354871699YG000_7798419T0000_1
。
I want to get the first part of while disregarding anything that comes after the 2nd underscore.我想获得第一部分,而忽略第二个下划线之后的任何内容。
One solution is to spilit all occurences of '_' and join the first two parts thogether like一种解决方案是拆分所有出现的 '_' 并将前两部分合并在一起,例如
string = "70354871699YG000_7798419T0000_1"
splits = re.split(r"_",string)
"_".join(splits[:2])
You could match the first underscore using a negated character [^_]
class first matching any char except _
and then match _
您可以使用否定字符
[^_]
类匹配第一个下划线,首先匹配除_
之外的任何字符,然后匹配_
Assert the second underscore using a positive lookahead:使用正向前瞻断言第二个下划线:
^[^_]+_[^_]+(?=_)
if you want to do this using python I would suggest:如果你想使用 python 来做到这一点,我建议:
string = "70354871699YG000_7798419T0000_1"
string = string.split("_",2)
string = string[2]
Not the best code, but hey it works.不是最好的代码,但嘿它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.