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

PL/SQL正则分割特定格式字符串问题求助

How to Split Transaction Strings with Regex into Consistent Elements

Let's fix this regex problem for you. Your goal is to extract 6 consistent elements from two types of transaction strings, even when some fields are empty—and your current regex is overcomplicating things with mismatched capture groups and limited word matching (since \w+ can't handle multi-word values like "movie tickets").

Step 1: Define the Target Formats

We need to handle two transaction structures:

  1. [Verb] [Amount] [Currency] in [Description] at [Merchant] on [Date] (all 6 elements filled)
  2. [Verb] [Amount] [Currency] to [Merchant] (Description and Date fields are empty)

Step 2: The Corrected Regex

This regex ensures both formats produce exactly 6 capture groups, with empty values where fields don't exist:

^(\w+) (\d+) ([A-Z]+)(?: in (.*?) at (.*?) on (.*)| to ()(.*)())$

Regex Breakdown:

  • ^: Anchor to the start of the string
  • (\w+): Group 1 – Capture the transaction verb (Spent/Paid)
  • (\d+): Group 2 – Capture the numeric amount
  • ([A-Z]+): Group 3 – Capture the uppercase currency code (CAD/EUR)
  • (?: ... | ... ): Non-capturing group for our two format branches:
    • Branch 1 (full transaction): in (.*?) at (.*?) on (.*)
      • in : Match the literal "in "
      • (.*?): Group 4 – Non-greedily capture multi-word descriptions (e.g., "movie tickets")
      • at: Match the literal " at "
      • (.*?): Group 5 – Non-greedily capture merchant names (e.g., "Cineplex")
      • on: Match the literal " on "
      • (.*): Group 6 – Capture the full date string
    • Branch 2 (simplified transaction): to ()(.*)()
      • to : Match the literal "to "
      • (): Group 4 – Capture an empty string (no description field)
      • (.*): Group 5 – Capture full merchant names (e.g., "Electric Company")
      • (): Group 6 – Capture an empty string (no date field)
  • $: Anchor to the end of the string

Step 3: Code Example (Python)

Here's how to use this regex to extract and format elements to match your desired output:

import re

# Define our regex pattern
transaction_pattern = r'^(\w+) (\d+) ([A-Z]+)(?: in (.*?) at (.*?) on (.*)| to ()(.*)())$'

# Test cases
test_transactions = [
    "Spent 30 CAD in movie tickets at Cineplex on 2018-06-01",
    "Paid 600 EUR to Electric Company"
]

for transaction in test_transactions:
    match = re.match(transaction_pattern, transaction)
    if match:
        # Replace None values with empty strings for consistency
        elements = [val if val is not None else "" for val in match.groups()]
        
        # Print formatted table
        print("ELEMENT ELEMENT_VALUE")
        print("------- -------------")
        for idx, value in enumerate(elements, 1):
            print(f"{idx:<7} {value}")
        print()  # Add spacing between test cases

Step 4: Expected Output

Running the code will produce exactly what you're looking for:

ELEMENT ELEMENT_VALUE
------- -------------
1       Spent
2       30
3       CAD
4       movie tickets
5       Cineplex
6       2018-06-01

ELEMENT ELEMENT_VALUE
------- -------------
1       Paid
2       600
3       EUR
4       
5       Electric Company
6       

Why Your Original Regex Failed

  • Mismatched Capture Groups: Your conditional regex created different numbers of groups for each format, leading to inconsistent element positions.
  • Limited Word Matching: Using \w+ only captures single words, so it couldn't handle multi-word values like "movie tickets" or "Electric Company".
  • Overcomplicated Logic: The conditional structure made the regex hard to debug and maintain—using a simple branch with aligned capture groups is much cleaner.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:56:09