-1

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'.

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)

Mazdak
  • 105,000
  • 18
  • 159
  • 188
user797963
  • 2,907
  • 9
  • 47
  • 88
  • @span, soboloven's string.startswith() answer is exactly what I needed. the question you referenced was helpful though, it's cool to know that any() exists and what it does. – user797963 May 22 '15 at 14:39
  • It's not clear what the requirement was, but in every case the question is a common duplicate. – Karl Knechtel Sep 25 '22 at 22:27

2 Answers2

2

If you are sure that something_tag is in the beggining you can try:

your_tag.startswith('something_tag')

If you are not sure about that:

res = 'something_tag' in your_tag
sobolevn
  • 16,714
  • 6
  • 62
  • 60
2

sobolevn bet me to it. 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.

eddiewould
  • 1,555
  • 16
  • 36