Excel中的RegEx函数

我经常使用Excel工作表,其中一些领域(观测)包含大量文本内容,部分结构化forms(至less在视觉上)

所以单个Cell / Obs的内容可能是这样的:

My name is John Doe I live at my address My Post code is ABC123 My Favorite Pet is: A dog 

在Excel中,我创build了一些函数,可以在单元格中查找string,所以可以说数据在“A2”的“A1”中,我可以使用“= GETPOSTCODE(A1)”,其中函数是:

 Function GetPostCode(PostCode As Range) As String regex.Pattern = "[AZ]{3}\d{3,}\b\w*" regex.IgnoreCase = True regex.MultiLine = True Set X = regex.Execute(PostCode.Value) For Each x1 In X GetPostCode = UCase(x1) Exit For Next End Function 

我可以用什么样的结构/function来完成这个任务? 单元格确实包含了比这更多的数据,例如它纯粹的,我有许多不同的“得到”函数与不同的正则expression式。

我已经对Greptypes的所有命令有了一个很好的看法,但是在有限的/正在开发的R技巧中挣扎着。

我一直在解决这种原则,但几乎停滞(其中textfield是与我的文本在明显的列!)我可以得到所有行的列表,其中包含邮政编码,但不是只是邮政编码:

 df$postcode <- df[(df$textfield = grep("[AZ]{3}\\d{3,}\\b\\w*", df$textfield), ] 

任何帮助赞赏!

我认为你需要regexprgrepexpr (findstring中的匹配)和regmatches来提取string的匹配部分:

 x <- "My name is John Doe I live at my address My Post code is ABC123 My Favorite Pet is: A dog" > regmatches(x, regexpr("[AZ]{3}\\d{3,}\\b\\w*", x, ignore.case = TRUE)) [1] "ABC123" 

其他选项可能包括来自stringr的str_extract或来自stringi包的stri_extract。