簡體   English   中英

如何使用正則表達式從文本中提取動態字符串

[英]How to extract dynamic string from a text using regular expression

我正在處理以下示例文本

  1. text here text here —(1) 在不損害先前授予任何現有股份或股份類別持有人的任何特殊權利的情況下,但受該法的約束,公司的股份可由董事發行。 (2) 第 (1) 款所述的股份可以作為董事發行優先權、遞延權或其他特殊權利或限制,無論是在股息、投票、資本返還或其他方面,但須遵守任何普通決議公司,確定。

我想按以下方式拆分文本

  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

但是,如果討論中的文本使用“等”。 或以點 (.) 結尾的縮寫,您必須枚舉它們以進行轉義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM