2017-07-30 1 views
-1

Je veux extraire les évaluations après la section dans le texte suivant. Ce pourrait être un mot ou deux mots. Par exemple «partiellement satisfaisant», «satisfaisant». Quel motif regex devrais-je utiliser? J'utilise mot vbaComment utiliser regex pour correspondre à un ou deux mots

Section 1 
Partly Satisfactory 
Some paragraphs inserted here 

Section 2 
Satisfactory 
Another paragraphs inserted here 

Section 3 
Partly Unsuccessful 
Another paragraphs inserted here 

Répondre

0

Le modèle peut être .: (Section \d+[\r\n])(\w+(?: \w+)?) par exemple.

Le premier groupe de capture intercepte la première ligne (Section, numéro et nouvelle ligne).

Le deuxième groupe de capture obtient la note (un ou deux mots) et c'est ce dont vous avez réellement besoin. Ci-dessous vous trouverez un exemple de script, coché (utilisé comme macro) sur un document 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

Merci beaucoup! C'est exactement ce que je cherche! –