keepcalculator

06 Numbers and Powers

Match First Occurrence: What It Means

Card No. 06·63425 · Updated · Standard formula, shown below

The calculator on this card is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

When a search or text-processing instruction says “match first occurrence,” it means the operation stops at the earliest position in the string where the pattern appears, ignoring any later matches. In practice, this is what most “find” functions do by default: they return the first hit, not all hits or the last one. The phrase “match first occurrence does not contain” usually appears in user questions about regex, Excel, or programming when the expected result is missing or unexpected.

Formula

The calculator above finds the character position of the first occurrence of a substring within a larger text. The formula is:

position = index of first character of the pattern in the text

Where:

  • Text – the string you are searching in (any length).
  • Pattern – the substring you are looking for (non-empty).
  • Position – the 1-based character index of the first match, or 0 if the pattern is not found.

This is the same logic used by most programming languages’ indexOf() or find() functions, and by spreadsheet functions like FIND() or SEARCH().

Worked Example

Using the default values from the calculator:

  • Text: “The quick brown fox jumps over the lazy dog”
  • Pattern: “fox”

Step 1: Start at the beginning of the text. Check each position for the pattern.

Step 2: The word “fox” appears at position 17 (counting from 1: T=1, h=2, e=3, space=4, q=5, u=6, i=7, c=8, k=9, space=10, b=11, r=12, o=13, w=14, n=15, space=16, f=17).

Step 3: Since this is the first (and only) occurrence, the result is 17.

The calculator returns 17, confirming the manual count.

Why “First Occurrence” Matters in Text Processing

When you search a document, a log file, or a dataset, you often only need the first time something appears. For example, finding the first error in a log, the first mention of a product in a review, or the first comma in a CSV line. The first occurrence is the default behavior in most “find” dialogs, but in programming, you sometimes have to explicitly ask for it (e.g., str.find() in Python returns the first, while str.findall() returns all).

Understanding this concept helps avoid bugs: if you assume a function returns the last occurrence or all occurrences, you may get incorrect results. For instance, in Excel, FIND(“a”, “banana”) returns 2 (the first “a”), not 4 or 6.

Common Scenarios Where “First Occurrence” Is Confusing

Users often ask “why doesn’t my search return the second match?” or “how do I get the last occurrence?” Here are typical cases:

  • Excel FIND vs. SEARCH – Both return the first occurrence, but SEARCH is case-insensitive and allows wildcards, while FIND is case-sensitive.
  • Regex in programming – The re.search() function in Python returns the first match, while re.findall() returns all. New users often confuse them.
  • Text editors – “Find Next” goes to the first occurrence from the cursor position, which may not be the very first in the file if you start in the middle.
  • SQL – INSTR() returns the first position of a substring, but LOCATE() can take a start position.

In all these, the key is to know whether the function returns the first match, all matches, or the last match.

How to Get the First Occurrence in Different Tools

If you need the first occurrence in a specific tool, here are the standard methods:

  • Excel/Google Sheets – Use FIND() or SEARCH(). Both return the position of the first match. To get the second, add a start position argument, e.g., FIND(“a”, A1, 3).
  • Python – Use str.find() for the first occurrence, str.rfind() for the last, and re.search() for regex first match.
  • JavaScript – indexOf() returns the first occurrence; lastIndexOf() returns the last.
  • Bash – grep -m1 stops after the first match per file.

When in doubt, consult the documentation for the specific function’s default behavior.

Why “Does Not Contain” Appears in Searches

The phrase “match first occurrence does not contain” often comes from a user trying to find a pattern but getting an empty result. Possible reasons:

  • The pattern is not present in the text at all.
  • The pattern is present but the search is case-sensitive and the case differs.
  • The pattern uses regex metacharacters that are not escaped.
  • The search starts from a position after the actual first occurrence.

For example, searching for “CAT” in “The cat sat” with case-sensitive matching returns nothing, even though “cat” is there. Always check case sensitivity and whether the pattern needs escaping.

Limitations and Pitfalls

First-occurrence searches have a few limitations:

  • Overlapping matches – If the pattern can overlap (e.g., “aba” in “ababa”), the first occurrence is still the earliest start, but subsequent matches may be tricky.
  • Zero-based vs. one-based indexing – Some languages (like JavaScript) return 0-based positions, while others (like Excel) return 1-based. This can cause off-by-one errors.
  • Whitespace and special characters – A space or newline might be part of the pattern, and invisible characters can prevent a match.

Always test with a simple example to confirm the indexing and case sensitivity.

Frequently Asked Questions

Why does my regex find the first match but not the second?

Most regex functions like re.search() return only the first match by design. To get all matches, use re.findall() or re.finditer(). If you need the second match specifically, you can iterate over matches and pick the one you want.

How do I find the first occurrence of a character in Excel?

Use =FIND(“char”, A1) for case-sensitive search, or =SEARCH(“char”, A1) for case-insensitive. Both return the position of the first occurrence. If the character is not found, they return a #VALUE! error.

What is the difference between first occurrence and last occurrence?

First occurrence is the earliest position where the pattern starts; last occurrence is the latest position. For example, in “banana”, the first “a” is at position 2, the last at position 6. Use lastIndexOf() in JavaScript or rfind() in Python to get the last.

Can I make a search return the first occurrence after a specific position?

Yes. In Excel, add a start position argument to FIND() or SEARCH(), e.g., FIND(“a”, A1, 5). In Python, use str.find(sub, start). In regex, use a lookbehind or manually slice the string.

Why does my find function return 0 when the text is empty?

An empty text has no characters, so no pattern can be found. Most functions return -1 (or 0 in some contexts) to indicate “not found.” Always check for empty strings before searching.

Related

Formula used
in t | Text to search | - | The quick brown fox jumps over the lazy dog | 1 | 1000
in p | Pattern to find | - | fox | 1 | 100
out pos | First occurrence position | character index | find(t, p) | 0
out found | Found? | - | if(find(t, p) > 0, 1, 0) | 0

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.

More cards in this drawer

The whole Numbers and Powers drawer →