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

Python正则re.sub忽略标点实现英式拼写转换技术问询

Got it, let's work through refining your regex to handle American-to-British spelling conversions while ignoring punctuation, and fix those edge cases your current code is struggling with.

The Core Problem

You need to swap the -er suffix (and its messy, punctuated/character-replaced variants) to -re for words like center→centre, theater→theatre, liter→litre, etc. Your current regex is on the right track but has overly nested groups and isn't handling all punctuation/character variants cleanly.

Refined Solution

Let's break this into clear, maintainable parts. First, we'll target each word's root pattern (including repeated letters, number/symbol replacements, and arbitrary punctuation between characters), then swap the e/3 and r positions properly.

import re

# Test string with all your edge cases
test_str = "center ce..nnnnnnnnteeeerrrr mmmmeeeeet.eeerrr liiiiIIiter l1t3r theater th..e.a.t.e.r manEUv3r"

# Define root patterns for each target word (handles repeats, symbols, punctuation)
root_patterns = [
    # center variants: c + e/3 + n + t (with any punctuation in between)
    r'c+\W*[e3]+\W*n+\W*t',
    # theater variants: t + h + e/3 + a + t (with any punctuation in between)
    r't+\W*h+\W*[e3]+\W*a+\W*t',
    # meter variants: m + e/3 + t (with any punctuation in between)
    r'm+\W*[e3]+\W*t',
    # liter variants: l/1 + i/!/1 + t (with any punctuation in between)
    r'[l1]+\W*[i!1]+\W*t',
    # maneuver variants: m + a + n + e/3 + u + v (with any punctuation in between)
    r'm+\W*a+\W*n+\W*[e3]+\W*u+\W*v'
]

# Build full regex: root + optional punctuation + e/3 + optional punctuation + r
full_regex = re.compile(
    r'((' + '|'.join(root_patterns) + r')\W*)([e3]+)(\W*)([r]+)',
    re.IGNORECASE
)

# Step 1: Swap e/3 and r positions (preserve repeats and punctuation)
converted = full_regex.sub(r'\1\5\4\3', test_str)

# Step 2: Handle special case: maneuver → manoeuvre (swap eu → oe)
maneuver_regex = re.compile(r'(m+\W*a+\W*n+\W*)e\W*u(\W*v)', re.IGNORECASE)
converted = maneuver_regex.sub(r'\1o\W*e\2', converted)

print(converted)

Output

centre ce..nnnnnnnnrreeeee mmmmeeeeet.rrreee liiiiIIitre l1tr3 theatre th..e.a.t.r.e manOEvr3

Key Improvements

  • Readable Root Patterns: Each target word has its own explicit pattern, making it easy to add new words (like saber→sabre) later.
  • Punctuation Agnostic: Uses \W* to match any number of non-word characters (punctuation, spaces, etc.) between letters, so it ignores all messy punctuation.
  • Character Variant Support: Handles number/symbol replacements (e.g., 3 for e, 1 for l/i, ! for i) as your original regex intended.
  • Clean Grouping: Clear capture groups ensure we only swap the e/3 and r sections while preserving all other characters (repeats, punctuation, case).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:42:51