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

SQL字符串分割需求:按规则提取FNAME、ALIAS1、ALIAS2字段

Alright, let's tackle how to extract the FNAME, ALIAS1, and ALIAS2 fields from the various full name formats you've shared. First, let's align on the input-output mapping with a clear table, then break down the rules, and finally share a working code example to implement this.

Input-Output Mapping

Here's how each input translates to the desired fields:

Full Name InputFNAMEALIAS1ALIAS2
ABC (PQR)ABCPQR
(ABC) PQR (XYZ)PQRABCXYZ
ABC @ PQRABCPQR
ABC @ PQR (XYZ)ABCPQRXYZ
ABC (PQR) @123ABCPQR123

Core Extraction Rules

Let's formalize the logic to match these examples:

1. How to get FNAME

  • If the string starts with a parenthesis: Skip the first bracketed content, then take the first non-blank, non-bracketed string that follows (like (ABC) PQR (XYZ) → PQR).
  • If there's a parenthesis not at the start: Take all content before the first ( (like ABC (PQR) → ABC).
  • If there are no parentheses: Take all content before the @ symbol (like ABC @ PQR → ABC).

2. How to get ALIAS1 and ALIAS2

Collect all relevant values in the order they appear in the string, then assign them to ALIAS1 (first match) and ALIAS2 (second match):

  • Relevant values include:
    • Any text inside parentheses (each () pair gives one value)
    • Any text immediately following @ (ignoring spaces after the @)
  • For example:
    • ABC @ PQR (XYZ): PQR (from @) comes first → ALIAS1; XYZ (from parentheses) comes next → ALIAS2
    • ABC (PQR) @123: PQR (from parentheses) comes first → ALIAS1; 123 (from @) comes next → ALIAS2

Working Code Example (Python)

Here's a Python script that implements this logic using regex to handle all the edge cases from your examples:

import re

def extract_name_fields(full_name):
    alias_candidates = []
    fname = ""

    # Handle cases where string starts with a parenthesis
    if full_name.startswith('('):
        # Extract the first bracketed content as an alias candidate
        first_bracket_match = re.match(r'\((.*?)\)', full_name)
        if first_bracket_match:
            alias_candidates.append(first_bracket_match.group(1))
        # Extract FNAME as the first non-blank, non-bracketed string after the first parenthesis
        fname_match = re.search(r'\)\s*(\w+)', full_name)
        if fname_match:
            fname = fname_match.group(1)
        # Extract any remaining bracketed content
        remaining_brackets = re.findall(r'(?<=\)\s*)\((.*?)\)', full_name)
        alias_candidates.extend(remaining_brackets)
    else:
        # Split the string at the @ symbol to handle both parts separately
        parts = re.split(r'@\s*', full_name)
        # Extract FNAME from the part before @ (or start of string)
        fname_match = re.match(r'(\w+)\s*(?:\(|$)', parts[0])
        if fname_match:
            fname = fname_match.group(1)
        # Extract bracketed content from the first part (before @)
        pre_at_brackets = re.findall(r'\((.*?)\)', parts[0])
        alias_candidates.extend(pre_at_brackets)
        # Handle the part after @ if it exists
        if len(parts) > 1:
            # Extract the first non-bracketed text after @
            post_at_text = re.match(r'(\w+)', parts[1])
            if post_at_text:
                alias_candidates.append(post_at_text.group(1))
            # Extract bracketed content from the after-@ part
            post_at_brackets = re.findall(r'\((.*?)\)', parts[1])
            alias_candidates.extend(post_at_brackets)

    # Assign alias values (default to empty string if no candidate exists)
    alias1 = alias_candidates[0] if len(alias_candidates) >= 1 else ""
    alias2 = alias_candidates[1] if len(alias_candidates) >= 2 else ""

    return (fname, alias1, alias2)

# Test the function with your examples
test_inputs = [
    "ABC (PQR)",
    "(ABC) PQR (XYZ)",
    "ABC @ PQR",
    "ABC @ PQR (XYZ)",
    "ABC (PQR) @123"
]

for input_str in test_inputs:
    result = extract_name_fields(input_str)
    print(f"{result[0]} {result[1]} {result[2]}".strip())

Running this code will output exactly the desired results:

ABC PQR
PQR ABC XYZ
ABC PQR
ABC PQR XYZ
ABC PQR 123

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:05:32