dポイントプレゼントキャンペーン実施中!

文字列中の両括弧を取り除く正規表現を求めています.

入力文は「(文字列1)文字列2」となっています.

両括弧は全角の丸括弧"(",")"と半角の丸括弧"(",")"のペアを見つけ,文字列2を取り出したいのです.

文字列1,文字列2は全角,半角の文字列(主に全角)が来ます.括弧の中に括弧が入れ子(ネスト)する事は想定していません.

例えば,「(グループA)田中」という入力に対しては,「田中」をExcel VBAで抽出したいのです.
正規表現ライブラリを使っていますが,別の方法でも効率良く抽出できるならOKです.


'---------------  以下を試した結果  ---------------------------------

Dim RE, strPattern As String, reMatch
Set RE = CreateObject("VBScript.RegExp")
' 'strPattern = "^(\(|().*(\)|))$"
' 'strPattern = "(?!.*[(|\(].+?[\)|)])" ' "(グループA)"にマッチする
' 'strPattern = "([\(.+?\)|(.+?)])"
' 'strPattern = "(^[(|\(].+?[\)|)])"
' strPattern = "^(?!.*[(|\(].+?[\)|)])" ' ""
' 'strPattern = "\(.+?\)"
' strPattern = "^(?!.*[\(.+?\)|(.+?)])" ' ""
' strPattern = "(?!.*[(.+?)])" ' ""
' strPattern = "(.+?)" ' "(グループA)"にマッチする
' strPattern = "^[?!.*((.+?))]" '
' strPattern = "[^((.+?))]" '
' '.Pattern = "^(?!.*xyz)"
' 'strPattern = "^(?!.*([.+?]))" ' ""
' strPattern = "[^(.*)]" '"グ","ル",,,
' strPattern = "^(?!([.*]).*)" ' ""
' strPattern = "^(?!(.*).*)" ' ""
' strPattern = "^(?!([.*]))" ' ""
strPattern = ").*$" ' ""

With RE
.Pattern = strPattern
.IgnoreCase = True
.Global = True
Set reMatch = .Execute(str)
If reMatch.Count > 0 Then
str = reMatch(0).Value
End If
End With

'開放
Set reMatch = Nothing
Set RE = Nothing

A 回答 (2件)

取り敢えず正規表現です。



Set RE = CreateObject("VBScript.RegExp")
strPattern = "(.*)|\(.*\)"
With RE
.Pattern = strPattern
.IgnoreCase = True
.Global = True

If .test(str) Then
str = .Replace(str, "")
End If
End With
    • good
    • 0
この回答へのお礼

回答ありがとうございました.
このとおりでできました.
(返信したつもりでしたが,書き込まれておりませんでした.失礼しました.)

お礼日時:2012/11/14 08:06

正規表現は"(\(|().*?(\)|))"です


意味は↓
半角左カッコまたは全角の左カッコ、
の後に文字が幾つか続き、直近の
半角右カッコまたは全角の右カッコ、
で終わる。
後はReplaceメソッドで、一致した所を
空文字列で置き換えます。
    • good
    • 0
この回答へのお礼

回答ありがとうございました.

お礼日時:2012/11/14 08:06

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています


このQ&Aを見た人がよく見るQ&A