简体   繁体   English

MS Excel:在字符串中的第 N 个和第 N 个字符之间提取

[英]MS Excel: Extract between the Nth and Nth Character in a string

Using an MS Excel formula (No VBA/Macro), I would like to extract between the Nth and Nth characters and/or letters in a string.使用 MS Excel 公式(无 VBA/宏),我想在字符串中的第 N 个和第 N 个字符和/或字母之间提取。 Example: I have text in Columns A2 and A3, I would like to extract text located between the 4th space and 9th space in each of the following strings.示例:我在 A2 和 A3 列中有文本,我想提取位于以下每个字符串中第 4 个空格和第 9 个空格之间的文本。

Column A2: Johnny went to the store and bought an apple and some grapes A2 列:约翰尼去商店买了一个苹果和一些葡萄

Column A3: We were not expecting to go home so early but we had no other choice due to rain A3 栏:没想到这么早回家,但因为下雨我们别无选择

Results:结果:

Column A2: store and bought an apple A2栏:储存并购买了一个苹果

Column A3: to go home so early A3栏:这么早回家

What you are looking for is a substring function, which is not really available in excel, I would suggest looking at this tutorial for functions that perform the same functionality https://www.excel-easy.com/examples/substring.html , and try to find the best one for your use case.您正在寻找的是一个子字符串函数,它在 excel 中并不真正可用,我建议您查看本教程以了解执行相同功能的函数https://www.excel-easy.com/examples/substring.html ,并尝试为您的用例找到最好的。 I would think the MID function would be the best suit for your use case.我认为 MID 功能最适合您的用例。

With Microsoft 365 or Excel 2019 you can use TEXTJOIN() in combination with FILTERXML() :在 Microsoft 365 或 Excel 2019 中,您可以将TEXTJOIN()FILTERXML()结合使用:

在此处输入图片说明

Formula in B1 : B1公式:

=TEXTJOIN(" ",,FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s[position()>4][position()<6]"))

The xpath expressions first selects all elements from the 5th word onwards and consequently only returns the first 5 elements from that respective array. xpath 表达式首先选择从第 5 个单词开始的所有元素,因此只返回相应数组中的前 5 个元素。 Therefor //s[position()>4][position()<6] can also be written as //s[position()>4 and position()<10] .因此//s[position()>4][position()<6]也可以写成//s[position()>4 and position()<10]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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