Python, how to compare substrings?

Question:

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)

Asked By: user797963

||

Answers:

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
Answered By: sobolevn

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.

Answered By: eddiewould
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.