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

匹配英镑现金金额的正则表达式:排除特定场景需求

Alright, let's build a regex that matches UK pound cash amounts while excluding the three specific scenarios you mentioned. Here's how to approach it:

Step 1: Define the core pound amount pattern

First, we need a regex that correctly matches standard pound formats—including integers, numbers with thousand separators, and decimal amounts (to two places):

£\d+(?:,\d{3})*(?:\.\d{2})?

Breakdown:

  • £: Matches the pound symbol directly
  • \d+: One or more digits (the whole number part)
  • (?:,\d{3})*: Optional thousand separators (non-capturing group, so we don't capture the separator itself)
  • (?:\.\d{2})?: Optional decimal part (exactly two digits, non-capturing)

Step 2: Add exclusions with lookarounds

Now we need to exclude the three unwanted scenarios using negative lookbehind and negative lookahead assertions:

Exclude amounts preceded by "worth"

Use a negative lookbehind to ensure the pound symbol isn't immediately preceded by "worth " (worth plus a space):

(?<!worth\s)

Exclude amounts followed by "voucher" (with or without an intermediate word)

Use a negative lookahead to block cases where the amount is followed by:

  • Directly " voucher" (scenario 2), or
  • Any single word + " voucher" (scenario 3)
(?!\s+(?:\w+\s)?voucher)

Breakdown:

  • \s+: One or more spaces (to separate the amount from the next word)
  • (?:\w+\s)?: Optional non-capturing group for a single word followed by a space (covers scenario 3)
  • voucher: The forbidden word itself (covers scenario 2 when the optional group is skipped)

Final Combined Regex

Putting it all together, the full regex is:

(?<!worth\s)£\d+(?:,\d{3})*(?:\.\d{2})?(?!\s+(?:\w+\s)?voucher)

Test Cases

Let's verify against your examples:

  • ✅ "Total £500 cash" → Matches (no forbidden prefix/suffix)
  • ✅ "£500 cash" → Matches (no forbidden prefix/suffix)
  • ✅ "Thing not worth much, £1 and some shoes" → Matches ("worth" isn't immediately before the pound amount)
  • ❌ "worth £200" → Doesn't match (preceded by "worth ")
  • ❌ "£200 voucher" → Doesn't match (followed directly by " voucher")
  • ❌ "£200 shop voucher" → Doesn't match (followed by " shop voucher")

Notes

  • This regex assumes standard spacing (spaces between words/amounts). If you need to handle extra whitespace (tabs, multiple spaces), replace \s with \s+ where appropriate, but the current version works for most common cases.
  • It doesn't handle edge cases like amounts in parentheses or with currency codes (e.g., "GBP 100"), but based on your examples, we're focusing on the £ symbol directly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 10:10:28