14

これはおそらく単純な問題ですが、残念ながら私は望んでいた結果を得ることができませんでした...

たとえば、次の行があります。

"Wouldn't It Be Nice" (B. Wilson/Asher/Love)

私はこのパターンを探す必要があります:

" (<any string>)

取得するには:

B. Wilson/Asher/Love

のようなものを試しまし"" (([^))]*))たが、うまくいかないようです。Match.Submatches(0)また、ブラケットに依存しているため、少し複雑になる可能性があるため、使用したいと思います...

4

6 に答える 6

25

編集:ドキュメントを調べた後、問題は、通常のスペースではなく、括弧の前に非改行スペースがあることです。したがって、この正規表現は機能するはずです:""[ \xA0]*\(([^)]+)\)

""       'quote (twice to escape)
[ \xA0]* 'zero or more non-breaking (\xA0) or a regular spaces
\(       'left parenthesis
(        'open capturing group
[^)]+    'anything not a right parenthesis
)        'close capturing group
\)       'right parenthesis

関数内:

Public Function GetStringInParens(search_str As String)
Dim regEx As New VBScript_RegExp_55.RegExp
Dim matches
    GetStringInParens = ""
    regEx.Pattern = """[ \xA0]*\(([^)]+)\)"
    regEx.Global = True
    If regEx.test(search_str) Then
        Set matches = regEx.Execute(search_str)
        GetStringInParens = matches(0).SubMatches(0)
    End If
End Function
于 2012-06-05T20:17:59.793 に答える
4

あなたの質問に対する厳密な答えではありませんが、時には、このシンプルで古き良き文字列関数は、正規表現よりも混乱が少なく、簡潔です。

Function BetweenParentheses(s As String) As String
    BetweenParentheses = Mid(s, InStr(s, "(") + 1, _
        InStr(s, ")") - InStr(s, "(") - 1)
End Function

使用法:

Debug.Print BetweenParentheses("""Wouldn't It Be Nice"" (B. Wilson/Asher/Love)")
'B. Wilson/Asher/Love

EDIT @alan は、これが曲のタイトルの括弧の内容と誤って一致することを指摘しています。これは、少し変更するだけで簡単に回避できます。

Function BetweenParentheses(s As String) As String
    Dim iEndQuote As Long
    Dim iLeftParenthesis As Long
    Dim iRightParenthesis As Long

    iEndQuote = InStrRev(s, """")
    iLeftParenthesis = InStr(iEndQuote, s, "(")
    iRightParenthesis = InStr(iEndQuote, s, ")")

    If iLeftParenthesis <> 0 And iRightParenthesis <> 0 Then
        BetweenParentheses = Mid(s, iLeftParenthesis + 1, _
            iRightParenthesis - iLeftParenthesis - 1)
    End If
End Function

使用法:

Debug.Print BetweenParentheses("""Wouldn't It Be Nice"" (B. Wilson/Asher/Love)")
'B. Wilson/Asher/Love
Debug.Print BetweenParentheses("""Don't talk (yell)""")
' returns empty string

もちろん、これは以前よりも簡潔ではありません。

于 2012-06-06T07:17:22.617 に答える
3

これは素晴らしい正規表現です

".*\(([^)]*)

VBA/VBScript の場合:

Dim myRegExp, ResultString, myMatches, myMatch As Match
Dim myRegExp As RegExp
Set myRegExp = New RegExp
myRegExp.Pattern = """.*\(([^)]*)"
Set myMatches = myRegExp.Execute(SubjectString)
If myMatches.Count >= 1 Then
    Set myMatch = myMatches(0)
    If myMatch.SubMatches.Count >= 3 Then
        ResultString = myMatch.SubMatches(3-1)
    Else
        ResultString = ""
    End If
Else
    ResultString = ""
End If

これは一致します

Put Your Head on My Shoulder

"Don't Talk (Put Your Head on My Shoulder)"  

更新 1

私はあなたのドキュメントファイルで正規表現を解放し、要求どおりに一致させました。正規表現が問題ないことは確かです。私はVBA/VBScriptに堪能ではありませんが、それが間違っていると思います

正規表現についてさらに議論したい場合は、それで問題ありません。私は難解に見えるこの VBscript API を掘り下げたいとは思っていません。

新しい入力が与えられると、正規表現が微調整されます

".*".*\(([^)]*)

引用符の中にある (Put Your Head on My Shoulder) と誤って一致しないように。

ここに画像の説明を入力

于 2012-06-05T19:13:02.747 に答える
0

より良いデータ ファイルが必要だと思います ;) パターンに適合しない外れ値がパターンに適合する場所に変更されるように、変更のためにファイルを一時ファイルに前処理することを検討することをお勧めします。少し時間がかかりますが、データ ファイルに一貫性がない場合は常に困難です。

于 2014-04-09T10:32:20.187 に答える