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

通过Google Calendar API提取自动创建事件中的航班确认号

Extracting Flight Confirmation Number from Google Calendar Event Description

Got it, let’s break down how to pull that flight confirmation number from your Google Calendar event’s description field—you’re already focused on the right spot, so let’s turn that into actionable steps:

1. Make Sure You’re Fetching the description Field

First, double-check your Events:get request includes the description field. The Google Calendar API doesn’t always return every field by default, so explicitly specify it with the fields parameter. For example, if calling the API directly:

GET https://www.googleapis.com/calendar/v3/calendars/{calendarId}/events/{eventId}?fields=description

If you’re using a client library (like Google’s Python SDK), ensure you aren’t filtering out the description when fetching the event object.

2. Parse the Description for the Confirmation Number

Flight confirmation numbers almost always follow predictable patterns—usually alphanumeric (6-10 characters) paired with keywords like "Confirmation Number" or "Booking Ref". Use regular expressions to target these patterns reliably.

Here’s a practical Python example to implement this:

import re

# Sample description from a calendar event (replace with your actual event data)
event_description = """
Flight: Boston → Denver
Confirmation Number: ABC1234
Departure: 2024-07-20 10:15 AM
"""

# List of common keyword patterns to match confirmation numbers
confirmation_patterns = [
    r'Confirmation Number:\s*([A-Z0-9]+)',
    r'Booking Ref:\s*([A-Z0-9]+)',
    r'Flight Confirmation:\s*([A-Z0-9]+)',
    r'Record Locator:\s*([A-Z0-9]+)'
]

extracted_number = None
for pattern in confirmation_patterns:
    # Case-insensitive search to cover variations like "confirmation number"
    match = re.search(pattern, event_description, re.IGNORECASE)
    if match:
        extracted_number = match.group(1)
        break

if extracted_number:
    print(f"Extracted Flight Confirmation Number: {extracted_number}")
else:
    print("No confirmation number found in the event description.")

Quick Tips for Better Matching:

  • Tweak the regex patterns to match the exact wording your airline uses (some might use "Booking Code" instead of "Ref").
  • If you don’t have consistent keywords, you can fall back to matching alphanumeric strings of typical confirmation length: r'\b[A-Z0-9]{6,10}\b'—just note this might catch random codes, so keyword matching is more reliable.

3. Handle Edge Cases

  • For round-trip flights with multiple confirmation numbers, use re.findall() instead of re.search() to capture all matches.
  • Add fallback logic to skip events with empty descriptions or no detected confirmation numbers, so your script doesn’t break unexpectedly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:12:44