如何根据正则表达式突出显示子字符串并将其转换为 Excel 或 HTML

How to highlight a substring based on a regex and turn it into Excel or HTML

我有以下数据框:

dat <-  structure(list(value = c("YMNSMQEML", "FIYRHMFCV", "VLFKFDMFI", 
"KLLDRFPVA", "RVLDDFTKL")), .Names = "value", row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))

dat
#>       value
#> 1 YMNSMQEML
#> 2 FIYRHMFCV
#> 3 VLFKFDMFI
#> 4 KLLDRFPVA
#> 5 RVLDDFTKL

给出以下 regex pattern L.{2}[FR] 我想创建一个 Excel,其中子字符串以粗体突出显示。

我怎样才能做到这一点?


更新 使用 LIKE 运算符:

Option Explicit
Sub boldSubString_LIKE_OPERATOR()
    Dim R As Range, C As Range
    Dim MC As Object
    Set R = Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp))

    For Each C In R
        C.Font.Bold = False
        If C.Text Like "L**F" Then
            Set MC = .Execute(C.Text)
            C.Characters(MC(0).firstindex + 1, MC(0).Length).Font.Bold = True
        End If
    Next C

End Sub

它在 Set MC = .Execute(C.Text) 中断,给出编译错误无效或不合格的引用。

既然你也提到了 HTML,你可能会生成一个 Rmarkdown HTML 记录并用 <b></b> 标签围绕模式。 使用 stringr 包中的 str_replace 函数的最小示例:

---
output:
  html_document: default
title: "Pattern"
---


```{r echo = FALSE}
library(stringr)

## your data
dat <-  structure(list(value = c("YMNSMQEML", "FIYRHMFCV", "VLFKFDMFI",
"KLLDRFPVA", "RVLDDFTKL")), .Names = "value", row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))


pattern <- "(L.{2}[FR])" # in brackets to reuse it in str_replace as 
## surround  group with the bold tag
dat$value <-  str_replace(dat$value, pattern, "<b>\1</b>")

knitr::kable(dat)
```

要在 Excel 中执行此操作,您可以访问 Range 对象的 Characters 属性:(并且内容需要是实际的字符串;而不是returns 一个字符串的公式)

Option Explicit
Sub boldSubString()
    Dim R As Range, C As Range
    Dim RE As Object, MC As Object
    Const sPat As String = "L.{2}[FR]"

'Range to be processed
Set R = Range(Cells(2, 1), Cells(Rows.Count, 1).End(xlUp))

'Initialize Regex engine
'Could use early binding if desireable
Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = False
    .ignorecase = True
    .Pattern = sPat

    For Each C In R
        C.Font.Bold = False
        If .test(C.Text) Then
            Set MC = .Execute(C.Text)
            C.Characters(MC(0).firstindex + 1, MC(0).Length).Font.Bold = True
        End If
    Next C
End With

End Sub