Extract first instance of two-character string between underscores

mnyankee

New Member
Joined
Mar 20, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I need help writing a formula that will search a string and return the first instance of two characters only, between two underscores. Probably not explaining that well but my examples are:

abcd_ef_gh_ijkl this should return ef
abc_efgh_ijk_lm_nop this should return lm

My current formula: =MID(A2,FIND("_",A2,1)+1,FIND("_",A2,FIND("_",A2,1)+1)-FIND("_",A2,1)-1)

is only returning the characters between the first two underscores. Is there a formula that will do what I am asking?
 
Hello, if you have access to the new REGEX functions then maybe:

Excel Formula:
=SUBSTITUTE(REGEXEXTRACT(A1,"_.{2}_"),"_","")
 
Upvote 0
Another option if you don't have regex yet
Fluff.xlsm
AB
1
2abcd_ef_gh_ijklef
3abc_efgh_ijk_lm_noplm
Sheet5
Cell Formulas
RangeFormula
B2:B3B2=LET(s,TEXTSPLIT(A2,"_"),TAKE(FILTER(s,LEN(s)=2),,1))
 
Upvote 0
Another approach (more like your original) and a shorter regex option

25 03 05.xlsm
ABC
1
2abcd_ef_gh_ijklefef
3abc_efgh_ijk_lm_noplmlm
Find 2
Cell Formulas
RangeFormula
B2:B3B2=MID(A2:A3,SEARCH("_??_",A2:A3)+1,2)
C2:C3C2=REGEXEXTRACT(A2:A3,"(?<=_)..(?=_)")
Dynamic array formulas.
 
Last edited:
Upvote 0
Solution
Another option if you don't have regex yet
Fluff.xlsm
AB
1
2abcd_ef_gh_ijklef
3abc_efgh_ijk_lm_noplm
Sheet5
Cell Formulas
RangeFormula
B2:B3B2=LET(s,TEXTSPLIT(A2,"_"),TAKE(FILTER(s,LEN(s)=2),,1))
While I am intrigued by your reply, I found an instance where it returns the wrong results. If my string is AA_BB_CC_DEF, your solution is returning AA when it should be BB. Seems to only happen when there are only two characters before the first underscore. Is that correctable?
 
Upvote 0
Another approach (more like your original) and a shorter regex option

25 03 05.xlsm
ABC
1
2abcd_ef_gh_ijklefef
3abc_efgh_ijk_lm_noplmlm
Find 2
Cell Formulas
RangeFormula
B2:B3B2=MID(A2:A3,SEARCH("_??_",A2:A3)+1,2)
C2:C3C2=REGEXEXTRACT(A2:A3,"(?<=_)..(?=_)")
Dynamic array formulas.
While I do not have regex, your first solution is working perfectly. I asked ChatGPT before posting my question and it was nowhere near the correct solution. Thanks!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top