Count Numbers in Text String: How To
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
To count numbers in a text string, you need to decide whether you mean individual digits (e.g., “abc123� has 3 digits) or whole numbers separated by non-numeric characters (e.g., “abc123 def45� has 2 numbers). This calculator counts whole numbers in any text you enter, and it also shows the total digits and the sum of all numbers found.
Formula
The calculator uses a regular expression to find sequences of digits, optionally with a decimal point, that are not part of another word. The pattern is \b\d+(\.\d+)?\b, which matches:
\b– a word boundary (start of a number)\d+– one or more digits(\.\d+)?– an optional decimal point followed by more digits\b– end of the number
Each match is counted as one number. The total digits are the sum of the lengths of all matched numbers (excluding the decimal point). The sum is the arithmetic total of all matched numbers.
Worked Example
Using the default text “apple 12, orange 34.5, banana�:
- Find matches: “12� and “34.5� → 2 numbers
- Digits: “12� has 2 digits, “34.5� has 3 digits → 5 total digits
- Sum: 12 + 34.5 = 46.5
The calculator shows 2 numbers, 5 digits, and a sum of 46.5.
When to Use This
You might need to count numbers in a text string when:
- You have a log file or survey response with embedded numbers and you want to know how many data points are present.
- You’re cleaning data and need to verify that each row contains the expected number of numeric values.
- You want to sum all prices, quantities, or measurements that appear in a text description.
This is not the same as counting digits. For example, “Room 404� has 3 digits but only 1 number. The distinction matters when you’re analyzing data that may contain multi-digit numbers.
How to Count Numbers in Excel or Google Sheets
If you prefer to do this in a spreadsheet, you can use a formula. For a cell A1 containing text, the following array formula counts numbers (in Excel, press Ctrl+Shift+Enter; in Google Sheets, just Enter):
=SUM(--ISNUMBER(VALUE(TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",100)),ROW(INDIRECT("1:"&LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1))*100-99,100)))))
This splits the text by spaces and counts how many pieces can be converted to numbers. It works for space-separated numbers, but not for commas or other delimiters. For a simpler approach, you can use a regular expression in Google Sheets with =COUNT(SPLIT(REGEXREPLACE(A1,"[^0-9.]+"," ")," ")) – but be careful: this also counts empty strings if the text starts or ends with a non-number.
Common Mistakes and Misconceptions
- Counting digits instead of numbers: “123� is one number, not three. Always clarify what you need.
- Including decimal points: “3.14� should be one number, not two. The regex above handles this.
- Negative numbers: “-5� is a number, but the pattern above would match “5� and ignore the minus sign. If you need negatives, use
-?\d+(\.\d+)?. - Commas as thousand separators: “1,000� would be split into “1� and “000� unless you remove commas first. Decide whether commas are delimiters or part of numbers.
Limitations of the Formula
This calculator treats a decimal point as part of a number only if it appears between digits. It does not handle:
- Scientific notation (e.g., 1e5)
- Commas as decimal separators (European format)
- Numbers written as words (e.g., “twenty-five�)
- Negative numbers unless you modify the pattern
For most real-world text, these limitations are acceptable. If you need to handle more complex formats, you would need a more sophisticated parser.
Frequently Asked Questions
How do I count numbers in a text string in Python?
Use the re module: import re; numbers = re.findall(r'\b\d+(\.\d+)?\b', text) – but note that findall returns the decimal part only if present. Better: re.findall(r'\b\d+(?:\.\d+)?\b', text) returns full matches. Then len(numbers) gives the count.
Does “123abc� count as one number or three?
It depends on your definition. This calculator counts it as one number (123) because the digits are contiguous. If you need to count digits, it would be three.
Can I count numbers in a text string in Notepad++?
Yes, use the Find dialog with regular expression mode. Search for \b\d+(\.\d+)?\b and check “Count� – it shows the number of matches.
What if my text has numbers with commas like “1,234�?
The calculator will count “1� and “234� as two numbers. To treat “1,234� as one, first remove the commas from the text, or use a pattern that allows commas: \b\d{1,3}(,\d{3})*(\.\d+)?\b.
Why does the sum show 46.5 for the default example?
Because 12 + 34.5 = 46.5. The calculator sums all numbers found. If you only need the count, ignore the sum.
Related
Formula used
in t | Text | - | apple 12, orange 34.5, banana | 0 out count | Numbers found | count | 3 | 0 out digits | Total digits | count | 3 | 0 out sum | Sum of numbers | - | 12 + 34.5 | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.