ThisCalculator

Everyday

Extract Text Between Parentheses: Methods & Tools

Updated · Standard formula, shown below

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.

This article explains how to extract text between parentheses in various contexts, including manual methods, spreadsheet formulas, and programming. You’ll learn the core techniques, see practical examples, and get answers to common questions about handling nested parentheses and edge cases.

Formula

The general formula to extract text between two delimiters (e.g., parentheses) is:

Extracted text = MID(text, FIND(left_delim, text) + 1, FIND(right_delim, text) – FIND(left_delim, text) – 1)

  • text – the original string (e.g., “(hello) world”)
  • left_delim – the opening character (e.g., “(“)
  • right_delim – the closing character (e.g., “)”)
  • FIND returns the position of a character within the text (1-based)
  • MID extracts a substring starting at a given position with a specified length

This works for the first occurrence of the delimiters. For multiple or nested parentheses, more advanced methods are needed.

Worked Example

Using the default values from the calculator:

  1. Text = “(hello) world”, left = “(“, right = “)”
  2. FIND(“(“, “(hello) world”) = 1
  3. FIND(“)”, “(hello) world”) = 7
  4. Length = 7 – 1 – 1 = 5
  5. MID(“(hello) world”, 1+1, 5) = MID(“(hello) world”, 2, 5) = “hello”

The calculator returns “hello” as the extracted text.

Why Extract Text Between Parentheses?

Parentheses are used in many types of content to add supplementary information: explanations, units, references, or alternative names. Extracting that content is useful for data cleaning, text analysis, or simply finding the key detail in a string. For example, from a product list like “Apple iPhone 14 (128GB)”, you might want to isolate “128GB” for inventory tracking.

Methods for Extraction

There are several ways to extract text between parentheses, depending on your tool:

  • Spreadsheet (Excel, Google Sheets): Use the MID/FIND formula shown above. For multiple parentheses, you may need to repeat the formula or use array formulas.
  • Programming (Python, JavaScript, etc.): Use regular expressions or string methods. For example, in Python: re.search(r'\((.*?)\)', text).group(1) extracts the first parenthetical content.
  • Text editors (Notepad++, VS Code): Use regex find-and-replace with a pattern like \((.*?)\) and replace with $1 to keep only the content.
  • Online tools: Many free online extractors exist, but be cautious with sensitive data.

Handling Nested and Multiple Parentheses

The simple formula only works for the first pair and does not handle nesting. For nested parentheses (e.g., “(a (b) c)”), you need a more sophisticated approach:

  • Stack-based algorithm: Iterate through the string, push when you see ‘(‘, pop when you see ‘)’. The text between the outermost parentheses can be captured.
  • Regular expressions with recursion: Some regex engines (e.g., PCRE) support recursive patterns like \(([^()]|\((?1)\))*\) to match nested groups.
  • Programming libraries: For example, Python’s pyparsing or JavaScript’s balanced-match package.

For multiple non-nested parentheses, you can loop through the string, finding each pair and extracting the content.

Common Mistakes and Edge Cases

  • Unbalanced parentheses: If the text has an opening parenthesis without a closing one, the formula returns an error. Always validate the input.
  • Multiple parentheses: The simple formula only extracts the first pair. To get all, you need to repeat or use a different method.
  • Different delimiter types: The same technique works for square brackets, curly braces, or custom delimiters—just change the characters.
  • Empty parentheses: If the parentheses are empty, the formula returns an empty string, which may be valid but should be handled.
  • Spaces and special characters: The extracted text may include leading/trailing spaces; use TRIM to clean it.

Choosing the Right Tool

Consider your context:

  • One-off task in a spreadsheet: Use the MID/FIND formula.
  • Regular data processing: Write a small script in Python or JavaScript.
  • Quick manual edit: Use a text editor’s regex find-and-replace.
  • No coding skills: Use an online extractor tool (but avoid uploading confidential data).

For most users, the spreadsheet formula is the easiest and requires no programming knowledge.

Frequently Asked Questions

How do I extract text between parentheses in Excel?

Use the formula =MID(A1, FIND("(", A1)+1, FIND(")", A1) - FIND("(", A1) - 1). This works for the first pair of parentheses. If there are multiple, you’ll need to copy the formula down or use a more complex array formula.

Can I extract text between parentheses in Python?

Yes, use the re module: import re; result = re.search(r'\((.*?)\)', text).group(1). This extracts the first parenthetical content. For all, use re.findall(r'\((.*?)\)', text).

What if there are nested parentheses?

For nested parentheses, a simple regex or formula will fail. Use a stack-based parser or a recursive regex pattern. In Python, you can use the regex module (not the standard re) with a recursive pattern.

How do I extract text between square brackets instead?

Simply replace the delimiters in the formula or regex. In Excel, change “(” to “[” and “)” to “]”. In regex, use \[(.*?)\].

Why does my formula return an error when there are no parentheses?

If the text does not contain the delimiters, FIND returns an error. You can wrap the formula in IFERROR to handle this: =IFERROR(MID(...), "").

Can I extract text between parentheses in Google Sheets?

Yes, Google Sheets supports the same MID and FIND functions. The formula works identically.

Related

Formula used
in t | Text | - | (hello) world | 0
in l | Left delimiter | - | ( | 0
in r | Right delimiter | - | ) | 0
out extracted | Extracted text | - | mid(t, find(t,l)+1, find(t,r)-find(t,l)-1) | 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.