仅在括号中读取文本c#

时间:2014-09-12 22:01:05

标签: c# regex text streamreader

我正在编写一个应用程序来读取文本文件,只读取以T#开头的括号内的文本。我很难做到这一切。我可以用

读出括号中的第一行
string fname = dialog.FileName; // selected file
                         string dispText= System.IO.File.ReadAllText (fname); // read text file
                         string output = dispText.Substring(dispText.IndexOf("(") + 1, dispText.IndexOf(")") - dispText.IndexOf("(") - 1);
                         label1.Text = output;

我尝试过使用类似结果的reg.expressions。

我的文字文件在下面。

 %
O13063131 ( P13063-01 and -03 ONLY, Pallet 1 OP10 )
(Part and Rev Number:  P13063-01 and -03 ONLY, Pallet 1 )
(OP Number:  OP10 )
(Programmer:   DATE : 05 - 16 - 14   )
(POST  Rev.E)

( T2 - IEM-C-AL 2.000X90XR.031X.492X4.00X5 ICR-T490 FLN D2.00-5-.75-R-13 )
( T3 - IEM-C-AL 1.00X90XR.016X.39X4.00X3 ICR-HM90 E90R-D1.00-3-W1.00XL )
( T7 - FEM-C-AL .500X1.25X3.0X3 NIA61563 )
( T10 - FEM-C-AL .375X1.25X3.00X3 MAF13840 )
( T13 - FEM-C-AL-0.250X0.75X2.50X3 NIA61543 )
( T16 - REM-C-AL-0.500XR0.125X1.250X3.00X3 NIA61655-125 )
( T20 - SDR-C-ST .500X120X1.25X3.0X2 FUL15377 )
( T30 - DRL-HSS-STD .397X135X3.75X5.125X2 CLE-C11652 )
( T56 - DRL-HSS-STD LETTER-H .266X135X2.50X4.10X2 CLE-C11636 )
( T18 - DRL-C-STD 6.3MMX118X2.44X4.00X2 SGS61094 )
( T24 - DRL-CO-STD-0.125X118X1.625X2.750X2 PTD010308 )
( T19 - 6-32 ROLL-FORM TAP / LOCATION 4B )
( T35 - DRL-HSS-STD .171X135X2.12X3.25X2 PTD-010311 )
( T40 - TAP-HSS-STD-RF .194-24-H6X.50X2.50X2 OSG-1400157100 )
( T57 - CSK-HSS-STD 1.00X82X0.00X1.0X3.25X3 MAF92100002 )
( T25 - CHM-C-STD .375X90X.040X1.00X2.50X2 MELIN-AMG-1212-DP )
G00 G91 G28 Z0.
G20
G0 G17 G40 G49 G80 G90
( ROUGH STK. -01 OP10 )
T2
M6 ( IEM-C-AL 2.000X90XR.031X.492X4.00X5 ICR-T490 FLN D2.00-5-.75-R-13 )
M11
M8
M88
G0 G90 X-6.039 Y13.74 B0. S7500 M1003
M10
G43 H2 Z12. T3

1 个答案:

答案 0 :(得分:2)

var matches = Regex.Matches(File.ReadAllText(filename), @"\( T\d+ .+?\)")
              .Cast<Match>()
              .Select(m => m.Value)
              .ToList();