简体   繁体   English

Python,如何比较子字符串?

[英]Python, how to compare substrings?

I'm trying to compare substrings, and if I find a match, I break out of my loop. 我正在尝试比较子字符串,如果找到匹配项,就会跳出循环。 Here's an example of a few strings: 这是一些字符串的示例:

'something_tag_05172015.3', 'B_099.z_02112013.1', 'something_tag_05172015.1' ,'BHO98.c_TEXT_TEXT_05172014.88'. 

The comparison should only compare the string I'm looking for, and everything in the same strings to what is to the left of the last underscore '_' in the strings. 比较仅应比较我要查找的字符串,以及相同字符串中的所有内容与字符串中最后一个下划线“ _”左侧的内容。 So, 'something_tag' should match only 'something_tag_05172015.3' and 'something_tag_05172015.1' . 因此, 'something_tag'应仅匹配'something_tag_05172015.3''something_tag_05172015.1'

What I did to do this was I split on the underscores and did a join on all elements but the last element in the split to compare against my test string (this drops everything to the right of the last underscore. Though it works, there's gotta be a better way. I was thinking maybe regex to remove the last underscore and digits, but it didn't work properly on a few tags. 我要做的是我在下划线处进行拆分,并对除拆分中的最后一个元素以外的所有元素进行联接,以与我的测试字符串进行比较(这将所有内容都放到了最后一个下划线的右边。我想过也许正则表达式删除最后一个下划线和数字,但是它在某些标签上无法正常工作。

Here's an example of the regex I was trying: re.sub('_\\d+\\.\\d+', '', string_to_test) 这是我尝试的正则表达式的示例: re.sub('_\\d+\\.\\d+', '', string_to_test)

If you are sure that something_tag is in the beggining you can try: 如果您确定something_tag在开始中,则可以尝试:

your_tag.startswith('something_tag')

If you are not sure about that: 如果您不确定:

res = 'something_tag' in your_tag

sobolevn bet me to it. sobolevn赌我对此。 For more complicated scenarios, use a regular expression with named-groups and/or non-capturing groups. 对于更复杂的情况,请使用带有命名组和/或非捕获组的正则表达式。

That way the overall string needs to match a specific format, but you can just pull out the sub parts that you're interested in. 这样,整个字符串就需要匹配特定的格式,但是您只需拉出您感兴趣的子部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM