【问题标题】:Count characters between specific tags/words计算特定标签/单词之间的字符
【发布时间】:2014-07-22 09:49:05
【问题描述】:

我有一个字符串不能超过 60 个字符的 Excel 文件。一些字符串受<br> 限制,而另一些则使用<break>。例如,我有这个单元格:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br> Lorem 
    ipsum dolor sit amet, consectetur adipiscing elit. Aliquam amet.<br>Lorem ipsum dolor 
    sit amet, consectetur adipiscing elit. Nulla eu sed.<break>Lorem ipsum dolor sit 
    amet, consectetur adipiscing elit.<br>

我希望 excel 或 Notepad++ 突出显示长度超过 60 个字符的标签之间的字符串,有什么“简单”的方法可以做到这一点吗?

谢谢!

【问题讨论】:

    标签: regex excel notepad++


    【解决方案1】:

    由于您使用 regexexcel 对其进行了标记,因此这里有一个 VBA 宏,它使用正则表达式来突出显示单元格中的长短语。我假设你上面的例子都在一个单元格中。每个超过 60 个字符的子字符串将(交替地)以红色或绿色突出显示。宏在您选择的单元格上运行。可以修改它以在其他范围上运行。

    编辑 代码更改为仅突出显示标签之间的字符串部分。在它还突出显示起始标记之前

    Option Explicit
    Sub HiglightGT60()
        Dim S As String
        Dim R As Range, C As Range
        Dim RE As Object, MC As Object, M As Object
        Const MaxLen As Long = 60
        Dim bColorFlag As Boolean
    
    Set R = Selection
    Set RE = CreateObject("vbscript.regexp")
        With RE
            .Global = True
            .Pattern = "(^|<br>|<break>)((?!<br>|<break>)[\s\S]){" & MaxLen + 1 & ",}"
            .MultiLine = False
        End With
    
    For Each C In R
        C.Font.Color = vbBlack
        S = C.Text
        If RE.test(S) = True Then
            Set MC = RE.Execute(S)
            For Each M In MC
                C.Characters(M.firstindex + 1 + Len(M.submatches(0)), _
                                M.Length - Len(M.submatches(0))) _
                                .Font.Color = IIf(bColorFlag, vbRed, vbGreen)
                bColorFlag = Not bColorFlag
            Next M
        End If
    Next C
    
    End Sub
    

    我不知道 Notepad++,但我想正则表达式会很相似

    【讨论】:

      【解决方案2】:

      对于 Notepad++ 恐怕我看不到否定符号组合的方法。你不能使用:

      ^(<br>|<break>)
      

      所以我能想到的下一个最好的事情就是做这样的事情:

      .{60}(<br>|<break>)
      

      但是,这只会突出显示最终以 br/break 结尾的行,但在 break 之前还有 60 个其他字符。如果末尾没有中断,则该行不会突出显示。

      如果这足够好,您也可以检查 \n:

      .{60}(<br>|<break>|\n)
      

      要突出显示只需按 Ctrl+F,切换到“标记”选项卡,将搜索模式设置为正则表达式并单击“全部标记”(不要忘记清除以前的标记)

      这是我能想到的使用 Notepad++ 的最佳选择,而且只需很少的努力。

      【讨论】:

        【解决方案3】:

        在 Excel 中,你可以试试这个小 VBA 代码:

        
        
        Private Sub CommandButton1_Click()
        Dim search_area As String
        search_area = "B3:B8"
        Dim separator As String
        separator = "<br>"
        Dim lengthLimit As Long
        lengthLimit = 60
        
        Dim text As Variant
        Dim i As Long
        Dim col As Long
        Dim row As Long
        
        For Each cell In ActiveSheet.range(search_area).Cells
            row = cell.row
            col = cell.Column + 1
            text = Split(cell, separator, -1)
            For i = 0 To UBound(text)
                If (Len(text(i)) > lengthLimit) Then
                    ActiveSheet.Cells(row, col) = text(i)
                    col = col + 1
                End If
            Next
        Next
        End Sub
        

        查看此记录,了解如何与 Excel 集成 https://dl.dropboxusercontent.com/spa/ymse6uo5hhtvqry/vba.gif

        • 按 Alt+F11 进入 VBA 模式。
        • 然后插入一个用户窗体并添加一个按钮。
        • 双击按钮并粘贴上面的代码。
        • 现在,按下播放按钮。点击按钮。

        假设长文本在 B3:B8 范围内,如果
        标签内的字符超过 60 个,则该段文本将显示在 B 列旁边的列中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-05
          • 2016-03-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多