C#中如何查找字符串匹配模式并截取包含该模式的片段
Got it, let's walk through how to solve this problem. You need to locate a specific substring pattern within a string, then pull out the entire space-separated segment that includes that pattern. Here are two straightforward approaches using Python:
Approach 1: Split and Iterate (Simple & Readable)
This method splits the input text into individual space-separated segments, then checks each one to see if it contains your target pattern. It’s easy to understand and works for most basic cases.
Code Example
text = "Some random words here EK/34 54/56/75 AB/12/34/56/BA1590/A and more random stuff..." whatImLookinFor = "12/34/56/" # Split the text into space-separated segments (handles spaces, tabs, newlines) segments = text.split() # Find the first segment containing the pattern target_segment = None for seg in segments: if whatImLookinFor in seg: target_segment = seg break # Remove this line if you want to collect all matching segments instead of just the first if target_segment: print(target_segment) # Output: AB/12/34/56/BA1590/A else: print("Pattern not found in any segment")
How It Works
text.split()breaks the string into a list of elements using any whitespace as the separator, so you don’t have to worry about multiple spaces or tabs.- We loop through each segment and check if your pattern is a substring of that segment.
- Once found, we store the segment and exit the loop (or keep iterating if you need all matches).
Approach 2: Regular Expressions (Flexible)
If you prefer a more concise solution or need to handle edge cases (like non-standard whitespace), regex is a great option. We’ll use a regex pattern that matches any sequence of non-space characters containing your target pattern.
Code Example
import re text = "Some random words here EK/34 54/56/75 AB/12/34/56/BA1590/A and more random stuff..." whatImLookinFor = "12/34/56/" # Escape the pattern to safely handle any special regex characters (like / in your example) escaped_pattern = re.escape(whatImLookinFor) # Regex to match any non-space sequence that includes the escaped pattern match = re.search(rf'\S*{escaped_pattern}\S*', text) if match: print(match.group()) # Output: AB/12/34/56/BA1590/A else: print("Pattern not found")
How It Works
re.escape()ensures any characters in your pattern that have special meaning in regex (like/) are treated as literal characters, avoiding unexpected behavior.- The regex
\S*{escaped_pattern}\S*matches:\S*: Zero or more non-space characters before the pattern- Your escaped target pattern
\S*: Zero or more non-space characters after the pattern
re.search()finds the first occurrence of this pattern in the text.
Edge Cases to Consider
- If the pattern appears in multiple segments, adjust the code to collect all matches (e.g., append to a list instead of breaking the loop).
- Leading/trailing spaces in the input don’t affect either method—
split()ignores them, and the regex skips over whitespace to find the target segment.
内容的提问来源于stack exchange,提问作者Lizard_Paszczyk

