如何匹配objectsA与objectsB中冒号分号分隔的数字(支持为空)
Hey there! Let's break down how to handle matching those numbers in objectsA and objectsB, including cases where either (or both) might be empty.
First, let's clarify the structure of your input string: it always follows objectsA=[some_triples];:objectsB=[some_triples], where [some_triples] is either empty or a semicolon-separated list of comma-triples (like 38155,54,1).
Approach 1: Grab Each Object's Content First (Most Straightforward)
The easiest way is to first extract the entire content of objectsA and objectsB separately, then parse those into numbers. This handles empty cases seamlessly.
Step 1: Use Regex to Capture Each Object's Content
Here's a regex that will grab everything after objectsA= and objectsB=, even if they're empty:
^objectsA=(.*?);:objectsB=(.*)$
^objectsA=locks onto the start of the string and theobjectsAkey.(.*?)is a non-greedy capture group that stops right at the;:objectsB=delimiter (so it gets nothing ifobjectsAis empty).;:objectsB=is the separator between the two objects.(.*)captures everything afterobjectsB=(again, empty ifobjectsBhas no values).
Step 2: Parse the Captured Content into Numbers
Let's use Python as an example to turn those captured strings into usable number lists:
import re # Your input string (or any variation with empty objects) input_str = "objectsA=38155,54,1;38155,53,1;38155,45,1;:objectsB=" # Match the regex against the input match = re.match(r'^objectsA=(.*?);:objectsB=(.*)$', input_str) if match: # Get the raw content for each object a_content = match.group(1) b_content = match.group(2) # Parse objectsA into a list of number triples objectsA = [] if a_content: # Split into individual triples, then split each triple into numbers objectsA = [[int(num) for num in triple.split(',')] for triple in a_content.split(';')] # Parse objectsB the same way objectsB = [] if b_content: objectsB = [[int(num) for num in triple.split(',')] for triple in b_content.split(';')] # Check the results print("objectsA numbers:", objectsA) print("objectsB numbers:", objectsB)
This code will output:
objectsA numbers: [[38155, 54, 1], [38155, 53, 1], [38155, 45, 1]] objectsB numbers: []
Approach 2: Directly Extract All Triples (If You Don't Care Which Object They're From)
If you just want all the triples across both objects without separating them, you can use this regex to find every comma-triple in the string:
\d+,\d+,\d+
Using re.findall() with this pattern will give you all the triples as strings, which you can then split into numbers:
all_triples = re.findall(r'\d+,\d+,\d+', input_str) all_numbers = [[int(num) for num in triple.split(',')] for triple in all_triples] print("All numbers:", all_numbers)
Handling Edge Cases
- Both objects empty: Input like
objectsA=;:objectsB=→ both parsed lists will be empty. - Only objectsA empty: Input like
objectsA=;:objectsB=123,45,6;789,0,1→objectsAis empty,objectsBhas two triples. - Only objectsB empty: Exactly your example input, which works perfectly with the code above.
Quick Tips
- Always check if the captured content is non-empty before splitting—this avoids errors from trying to split an empty string.
- Use non-greedy quantifiers (
.*?) in regex to make sure you don't accidentally capture the;:objectsB=delimiter as part ofobjectsA's content.
内容的提问来源于stack exchange,提问作者webmasterdro

