You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何匹配objectsA与objectsB中冒号分号分隔的数字(支持为空)

How to Match Numbers in objectsA and objectsB (Even When One or Both Are Empty)

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 the objectsA key.
  • (.*?) is a non-greedy capture group that stops right at the ;:objectsB= delimiter (so it gets nothing if objectsA is empty).
  • ;:objectsB= is the separator between the two objects.
  • (.*) captures everything after objectsB= (again, empty if objectsB has 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 → objectsA is empty, objectsB has 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 of objectsA's content.

内容的提问来源于stack exchange,提问作者webmasterdro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:23:45