Regex 如何使用正则表达式从文本中提取动态字符串

Regex 如何使用正则表达式从文本中提取动态字符串,regex,vba,parsing,Regex,Vba,Parsing,我正在编写下面的示例文本 在此文本—(1)在不损害先前授予任何现有股份或任何类别股份持有人的任何特殊权利的情况下,根据《公司法》,公司股份可由董事发行。(2) 根据本公司的任何普通决议,第(1)款所述股份的发行可具有优先权、递延权或其他特殊权利或限制,无论是关于股息、投票权、资本回报还是其他方面 我想按以下方式拆分文本 文本在这里文本在这里- (1) 在不损害先前授予任何现有股份或任何类别股份持有人的任何特殊权利的情况下,根据《公司法》,公司股份可由董事发行 (2) 根据本公司的任何普通决议,第

我正在编写下面的示例文本

  • 在此文本—(1)在不损害先前授予任何现有股份或任何类别股份持有人的任何特殊权利的情况下,根据《公司法》,公司股份可由董事发行。(2) 根据本公司的任何普通决议,第(1)款所述股份的发行可具有优先权、递延权或其他特殊权利或限制,无论是关于股息、投票权、资本回报还是其他方面
  • 我想按以下方式拆分文本

  • 文本在这里文本在这里-
  • (1) 在不损害先前授予任何现有股份或任何类别股份持有人的任何特殊权利的情况下,根据《公司法》,公司股份可由董事发行

    (2) 根据本公司的任何普通决议,第(1)款所述股份的发行可具有优先权、递延权或其他特殊权利或限制,无论是关于股息、投票权、资本回报还是其他方面

    文本本质上是动态的,而不是
    (1)
    (2)
    ,我们可以得到
    (a)
    (b)
    a.
    b.
    i
    ii
    iii
    。为了处理第一个问题陈述,我在VBA中使用了以下正则表达式:

    Pattern = "([(][\d][)])([A-Z,a-z,.,\-,’,(,),_, ,:,\n,“,”,"",:,;,—,-,\—,\t,\r,]*)"
    

    我正在寻找分割内容的解决方案,但没有寻找VBA或正则表达式中特定的解决方案。我们也非常感谢您使用任何其他方法。

    您没有回答澄清问题,但请尝试下一个VBA功能:

    Function SpecSplitText(x As String) As String
      Dim strFin As String, strInt As String, arr, arr1, El
      
      arr = Split(x, "—"):
      strFin = arr(0) & vbCrLf
      arr1 = Split(arr(1), ".")
      
      For Each El In arr1
        If Len(LTrim(El)) = 1 Then
            strInt = LTrim(El) & ". "
        Else
            strFin = strFin & strInt & LTrim(El) & "." & vbCrLf
            strInt = ""
        End If
      Next
      strFin = left(strFin, Len(strFin) - 3)
      SpecSplitText = strFin
    End Function
    
    您可以使用下一个简单测试
    Sub

    Sub testSpecSplitText()
        Dim x As String, y As String, z As String
        x = "7. text here text here —(1) Without prejudice to any special rights previously conferred on the holders of any existing shares or class of shares but subject to the Act, shares in the company may be issued by the directors. (2) Shares referred to in paragraph (1) may be issued with preferred, deferred, or other special rights or restrictions, whether in regard to dividend, voting, return of capital, or otherwise, as the directors, subject to any ordinary resolution of the company, determine."
        y = "8. text here text here —a. Without prejudice to any special rights previously conferred on the holders of any existing shares or class of shares but subject to the Act, shares in the company may be issued by the directors. b. Shares referred to in paragraph (1) may be issued with preferred, deferred, or other special rights or restrictions, whether in regard to dividend, voting, return of capital, or otherwise, as the directors, subject to any ordinary resolution of the company, determine."
        z = "9. text here text here —I Without prejudice to any special rights previously conferred on the holders of any existing shares or class of shares but subject to the Act, shares in the company may be issued by the directors. II Shares referred to in paragraph (1) may be issued with preferred, deferred, or other special rights or restrictions, whether in regard to dividend, voting, return of capital, or otherwise, as the directors, subject to any ordinary resolution of the company, determine."
        
        Debug.Print SpecSplitText(x)
        Debug.Print SpecSplitText(y)
        Debug.Print SpecSplitText(z)
    End Sub
    

    但是,如果讨论中的文本使用“etc”或以点(.)结尾的缩写,则必须枚举它们以转义。

    要拆分字符串,必须明确确定拆分规则。是否要拆分第一个字符串以提取每个命题。我是说每个部分都以点结尾,除了第一个?然后用“-”分开?是否有其他适用的规则?当您说“不寻找VBA或正则表达式中特定的解决方案”时,您有什么想法,因为您将问题标记为
    VBA
    regex
    ?那么,从哪里提取这段文字呢?它们是Word中的段落还是什么?@Lal Ansari:你没有找到同样的时间来测试上面的代码吗?