Python:如何判断指定短语是否按原顺序精确出现在字符串中
Check if a Phrase Appears in Exact Order in a Text String
Hey there! Let's break down how to solve this problem—you want to verify if a given phrase shows up in a text string in the exact order of its words, not just having all the words present somewhere in the text. Here's a straightforward approach with code examples to make it clear.
Core Idea
The key is to:
- Split your target phrase into individual words (handling punctuation as needed).
- Traverse the text string to find each word from the phrase one by one, making sure each subsequent word appears after the previous one ends. If any word is missing or out of order, the phrase doesn't match.
Python Implementation Example
Here's a reusable function that does exactly this:
def is_phrase_in_order(text, phrase): # Clean up phrase words: split into words and strip surrounding punctuation phrase_words = [word.strip('.,!?;:') for word in phrase.split()] current_position = 0 for word in phrase_words: # Look for the word starting from the current position in the text word_start = text.find(word, current_position) if word_start == -1: # Word not found after the previous one—no match return False # Update position to end of the found word, so next word is searched after this point current_position = word_start + len(word) # All words found in order return True # Test with your example sample_text = 'This question will probably be down voted because I am not able to post a minimal, complete, and verifiable example.' sample_phrase = 'I will not be down voted.' print(is_phrase_in_order(sample_text, sample_phrase)) # Output: False (matches your expected result)
How This Works
- Phrase Cleaning: We strip common punctuation from each word in the phrase to avoid mismatches (like "voted." in the phrase vs "voted" in the text). Adjust the punctuation list in
strip()if you need to handle other characters. - Sequential Search: For each word in the phrase, we use
text.find(word, current_position)to look for it only after the end of the last found word. This ensures the order is preserved. - Early Exit: If any word can't be found after the previous one, we immediately return
Falseinstead of checking further—saves time!
Customization Tips
- Case Insensitivity: If you don't care about uppercase/lowercase differences, convert both the text and phrase to lowercase before processing:
text = text.lower() phrase = phrase.lower() - Exact Punctuation Matching: If punctuation is part of the required match (e.g., you need "down voted," with a comma), remove the
strip()step to match words exactly as they are. - Continuous Substring Check: If you actually need the phrase to appear as a continuous block of text (not just ordered words), you can simplify to just
return phrase in text—but that's not what your example calls for.
Test a Valid Case
Let's try a phrase that does match the order in your sample text:
valid_phrase = 'will probably be down voted' print(is_phrase_in_order(sample_text, valid_phrase)) # Output: True
内容的提问来源于stack exchange,提问作者FaCoffee
相关产品推荐
相关产品推荐

