我想匹配一个类似目录的变量列表,例如:
Same/Same2/Foot/Ankle/Joint/Actuator/Sensor/Temperature/Value=4.123
Same/Same2/Battery/Name=SomeString
Same/Same2/Home/Land/Some/More/Stuff=0.34
“子目录”的长度是可变的,有一个上限(高于9)。 我想把每个子目录分组,除了上面我命名为“相同”的第一个子目录。在
我能想到的最好办法是:
^{pr2}$它已经在寻找4-8个子目录,但只对最后一个进行分组。为什么? 有没有更好的解决方案使用群体量词?在
编辑:已解决。将改用split()。在
只使用分割?在
另外,如果你想要密钥/val对,你可以这样做。。。在
^{pr2}$编辑1
现在您已经给出了更多关于您想要获得什么的信息(“Same/Same2/Battery/Name=SomeString将变为Same2_Battery_Name=SomeString”\u)可以提出更好的解决方案:使用regex或使用split(),+replace()
^{pr2}$结果
编辑2
重读你的评论,我意识到我没有考虑到你想把位于“=”号之前而不是后面的字符串部分放在上面。在
因此,这段新代码公开了3个满足此要求的方法。您将选择您喜欢的:
结果,在Windows平台上
我可能误解了您到底想做什么,但下面是您在没有regex的情况下应该如何做:
相关问题 更多 >
编程相关推荐