2016-09-05 8 views
-2

私は私がやりたいことの例[Fruit]または[Pears]角括弧の間にある正規表現の出現を見つけるにはどうすればよいですか?

ため[]との間にあるすべてのテキストは、したがって、この2つの部分の質問

です見つけるれ Microsoft.Office.Interop.Word

word.Application wordApp = new word.Application { Visible = true }; 
word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: true); 
aDoc.Activate(); 

を使用してWord文書を開いています

  • [doc]内のすべてのテキストをどのように見つけるか[]
  • 適切な正規表現とは角括弧の間nythingは[]
+0

は考えていない、なぜdownvotesので、我々ワード例と私はそれがワイルドカードを使用するようにしてください – JL1

+0

受け入れるだろう:[リンク]( http://stackoverflow.com/a/1501545/5045688)。 –

答えて

0

するTry MatchCollection

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication1 
{ 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      string input = "[Fruit] or [Pears]"; 
      string pattern = @"\[(?'fruit'[^\]]+)\]"; 

      MatchCollection matches = Regex.Matches(input, pattern); 
      foreach (Match match in matches) 
      { 
       Console.WriteLine("fruit : '{0}'", match.Groups["fruit"].Value); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 
+0

これはどういう意味なのですか?単語でどうやってやればいいですか? – JL1

+0

私は、単語の文書にすべてのテキストを取り込むというnawfalの解決策が好きです。私の正規表現を適用する:http://stackoverflow.com/questions/9181085/how-to-get-text-from-line-number-in-ms-word – jdweng

0
string str = "[first] second [third]"; 
var split = Regex.Matches(str, @"\[([A-Za-z])\w+\]"); 
List<string> result = new List<string>(); 
foreach (Match match in split) 
{ 
     result.Add(match.Value.TrimStart('[').TrimEnd(']')); 
} 
+1

あなたはどうやってそれを単語でしますか? – JL1