2011-10-31 10 views
2

[]の間に文章を出すには、正規表現が必要です。Regexでテキストを検索する

例テキスト:

Hello World - Test[**This is my string**]. Good bye World. 

望ましい結果:

**This is my String** 

私が出ている正規表現はTest\\[[a-zA-Z].+\\]であるが、これは全体の**Test[This is my string]**を返します。

答えて

1
(?<=Test\[)[^\[\]]*(?=\]) 

あなたがしたいことをする必要があります。

(?<=Test\[) # Assert that "Test[" can be matched before the current position 
[^\[\]]* # Match any number of characters except brackets 
(?=\])  # Assert that "]" can be matched after the current position 

lookaround assertionsで読んでください。 JavaScriptを使用して概念の

\[([^]]+)\] 

迅速な証拠:

+0

ありがとうございます...魅力的です。 – Jakes

2

あなたは関心のテキストにアクセスするためのキャプチャグループを使用することができ

var text = 'Hello World - Test[This is my string]. Good bye World.' 
var match = /\[([^\]]+)\]/.exec(text) 
if (match) { 
    console.log(match[1]) // "This is my string" 
} 

正規表現エンジンあなたがサポートを使用している場合は、両方のlookaheadおよびlookbehind、Timのソリューションがより適切です。

2
Match m = Regex.Match(@"Hello World - Test[This is my string]. Good bye World.", 
      @"Test\[([a-zA-Z].+)\]"); 
Console.WriteLine(m.Groups[1].Value); 
関連する問題