简体   繁体   中英

Extract Multiple Sub-strings Between Brackets/Parentheses in Excel

I have various strings (formatted as text) that have a similar syntax in column F. Eg:

1t ttn TEST FAILED (9-5 passOne, 21-7 & 877-12 passTwo)

I want to extract the numbers within the brackets into separate cells. There may be anywhere between 1 and 5 different numbers.

The numbers will always contain a dash (-) somewhere in the middle and may have up to 3 digits either side of the dash.

Is this possible? I've been through previous questions and can't find anything that answers this.

Edit to show entered JvdV formula:

在此处输入图像描述

With Excel 2016:

在此处输入图像描述

Formula in B1 :

=SUBSTITUTE(FILTERXML("<t><s>`"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(REPLACE($A1,1,FIND("(",$A1),""),")",""),"&","&")," ","</s><s>`")&"</s></t>","//s[contains(., '-')][translate(., '`-','')*0=0]["&COLUMN(A1)&"]"),"`","")

Confirm through Ctrl Shift Enter

Drag right, and down for all lines of data.


This assumed:

  • Substring of interest (between paranthesis) is always at the end.
  • Numbers with the hyphen as a delimiter may occur outside this substring and are not wanted in our results.

The trick here is to first sanitize the string and find the opening-paranthesis and remove everything prior to that using REPLACE() . Then, just SUBSTITUTE() the closing-paranthesis. The remainder can be split on spaces using FILTERXML() . To prevent errors and unwanted results I subsituted the ampersand and included accent graves to prevent Excel to recognize these substrings as dates. For more insight on the used xpath and the workings of FILTERXML() I'd like to refer you to this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM