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

如何用Excel公式或VBA提取文本字符串中的最大数值

Extracting the Largest Dollar Amount from Your Text

Hey Felix, sorry Scott's solution didn't pan out for you—let's get this sorted! Here's a straightforward, reliable approach to pull the maximum numeric value from your formatted text, regardless of the extra context around each entry.

Step-by-Step Breakdown

  1. Match all dollar amounts: Use a regular expression to target every value prefixed with $, including those with commas (like $500,000).
  2. Clean the values: Strip out the $ and commas so we can convert the strings to numeric values.
  3. Find the maximum: Compare all the cleaned numeric values to grab the largest one.

Example Code (Python)

Let's use your sample text to demonstrate:

import re

sample_text = "Class 1 - $250,000 - PTD equal to principal sumClass 2 - $500,000 - PTD equal to principal sumClass 3 - $500,000 - PTD equal to principal sumClass 4 - $250,000 Class 5 - $250,000 Class 6 - $250,000"

# Match all dollar amounts (formatted with optional commas)
dollar_matches = re.findall(r'\$\d{1,3}(?:,\d{3})*', sample_text)

# Clean each match: remove $ and commas, convert to integer
numeric_values = [int(match.replace('$', '').replace(',', '')) for match in dollar_matches]

# Get the maximum value
max_amount = max(numeric_values)

print(f"Largest amount: ${max_amount:,}")  # Output: Largest amount: $500,000

How This Works

  • The regex \$\d{1,3}(?:,\d{3})* specifically looks for:
    • A $ symbol
    • Followed by 1-3 digits (the first part of the number)
    • Optional groups of , plus 3 digits (for thousands separators)
  • Converting to integers lets us easily compare values to find the maximum.
  • The final print statement adds back the $ and comma formatting for readability.

If You're Using Another Language

The core logic stays the same:

  • Use regex to capture all $-prefixed values
  • Clean the string to remove non-numeric characters (except digits)
  • Convert to numbers and find the max

For example, in JavaScript:

const sampleText = "Class 1 - $250,000 - PTD equal to principal sumClass 2 - $500,000 - PTD equal to principal sumClass 3 - $500,000 - PTD equal to principal sumClass 4 - $250,000 Class 5 - $250,000 Class 6 - $250,000";
const dollarMatches = sampleText.match(/\$\d{1,3}(?:,\d{3})*/g);
const numericValues = dollarMatches.map(match => parseInt(match.replace(/[$,]/g, '')));
const maxAmount = Math.max(...numericValues);
console.log(`Largest amount: $${maxAmount.toLocaleString()}`); // Output: Largest amount: $500,000

Give this a try—it should work perfectly with your sample text and any similar formatted content you have!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:21:59