2017-07-30 5 views
-1

以下のテキストのセクションの後ろにレーティングを抽出します。 1つの単語か2つの単語のどちらかになります。例えば、「部分的に満足」、「満足」など。どの正規表現パターンを使うべきですか?私は、パターンを例えばすることができます。:(Section \d+[\r\n])(\w+(?: \w+)?)regexを使用して1つまたは2つの単語を一致させる方法

Section 1 
Partly Satisfactory 
Some paragraphs inserted here 

Section 2 
Satisfactory 
Another paragraphs inserted here 

Section 3 
Partly Unsuccessful 
Another paragraphs inserted here 

答えて

0

ワードVBAを使用しています。

最初のキャプチャグループは、最初の行(セクション、番号と改行)をキャッチします。

第2のキャプチャグループは、評価(1つまたは2つの単語) をキャッチし、これが実際に必要なものです。

以下は、Word文書でチェックされた(マクロとして使用される)サンプルスクリプトです。

Sub Re() 
    Dim pattern As String: pattern = "(Section \d+[\r\n])(\w+(?: \w+)?)" 
    Dim regEx As New RegExp 
    Dim src As String 
    Dim ret As String 
    Dim colMatches As MatchCollection 
    Dim objMatch As Match 

    ActiveDocument.Range.Select 
    src = ActiveDocument.Range.Text 
    Selection.StartOf 

    With regEx 
    .Global = True 
    .MultiLine = True 
    .pattern = pattern 
    End With 
    If (regEx.Test(src)) Then 
    Set colMatches = regEx.Execute(src) 
    ret = "Matches " & colMatches.Count & ": " 
    For Each objMatch In colMatches 
     ret = ret & vbCrLf & objMatch.SubMatches(1) 
    Next 
    Else 
    ret = "Matching Failed" 
    End If 
    MsgBox ret, vbOKOnly, "Result" 
End Sub 
+0

ありがとうございます!それはまさに私が探しているものです! –

関連する問題