如何使用Regex按1B、2E、1D、4E规则对数值字符串分组?
Solution for Grouping the String by 1B, 2E, 1D, 4E Rule
Got it, let's tackle this grouping problem step by step. Based on your example, the grouping logic is clear: we need to split the comma-separated string into four ordered groups:
- 1B: All items ending with
B - 2E: All
E-ending items that appear before anyDitems - 1D: All items ending with
D - 4E: All remaining
E-ending items that come after theDitems
Here are two practical solutions you can use:
Method 1: String Splitting + Iterative Grouping (Flexible & Easy to Maintain)
This approach is straightforward and works even if the number of items in each group varies (as long as the order of B→E→D→E stays consistent).
Steps:
- Split the input string into a list of individual items using
,as the delimiter. - Iterate through the list, routing each item to the correct group based on its suffix and the current group we're collecting.
Code Example (Python):
s1 = "7.51B, 8.01B, 7.02E, 7.52E, 8.02E, 7.01D, 7.51D, 8.01D, 8.54E, 9.04E, 9.54E, 10.04E, 10.54E, 11.04E" # Split and clean up the items items = [item.strip() for item in s1.split(',')] # Initialize empty groups group_1b = [] group_2e = [] group_1d = [] group_4e = [] # Track which group we're currently adding items to current_group = group_1b for item in items: if current_group == group_1b: if item.endswith('B'): group_1b.append(item) else: # Switch to 2E group and add the current item group_2e.append(item) current_group = group_2e elif current_group == group_2e: if item.endswith('E'): group_2e.append(item) else: # Switch to 1D group and add the current item group_1d.append(item) current_group = group_1d elif current_group == group_1d: if item.endswith('D'): group_1d.append(item) else: # Switch to 4E group and add the current item group_4e.append(item) current_group = group_4e else: # Add all remaining items to 4E group group_4e.append(item) # Format the output as requested formatted_output = ( f"1B - {', '.join(group_1b)};\n" f"2E - {', '.join(group_2e)};\n" f"1D - {', '.join(group_1d)};\n" f"4E - {', '.join(group_4e)}" ) print(formatted_output)
Method 2: Regular Expression Matching (For Fixed Input Format)
If you know the input string will always follow the exact order of B→E→D→E with no unexpected variations, you can use a regex to directly capture each group in one go.
Regex Pattern Explanation:
(?:\d+\.\d+[BED], )*: Non-capturing group to match repeated items (e.g.,7.51B,)\d+\.\d+[BED]: Matches a single item (like8.01B)- Four capturing groups correspond to the four target groups.
Code Example (Python):
import re s1 = "7.51B, 8.01B, 7.02E, 7.52E, 8.02E, 7.01D, 7.51D, 8.01D, 8.54E, 9.04E, 9.54E, 10.04E, 10.54E, 11.04E" # Regex pattern to capture all four groups pattern = r'^((?:\d+\.\d+B, )*\d+\.\d+B), ((?:\d+\.\d+E, )*\d+\.\d+E), ((?:\d+\.\d+D, )*\d+\.\d+D), ((?:\d+\.\d+E, )*\d+\.\d+E)$' match_result = re.match(pattern, s1) if match_result: # Extract each captured group group_1b = match_result.group(1) group_2e = match_result.group(2) group_1d = match_result.group(3) group_4e = match_result.group(4) # Format and print the output formatted_output = ( f"1B - {group_1b};\n" f"2E - {group_2e};\n" f"1D - {group_1d};\n" f"4E - {group_4e}" ) print(formatted_output) else: print("Input string doesn't match the expected format!")
Which Method to Choose?
- Use Method 1 if you need flexibility (e.g., the number of items in each group might change, or you want to handle edge cases easily).
- Use Method 2 if your input format is strictly fixed and you prefer a more concise regex-based approach.
内容的提问来源于stack exchange,提问作者Sathya Narayana
相关产品推荐
相关产品推荐

