使用正则表达式提取字符串

时间:2015-10-06 05:53:57

标签: c# regex vb.net

我有来自文本文件的以下文字:

This is a Test, Section test TS 544 Ord 0 (1246788-5)   [2]
This is a Test2, Section test TS 544 Ord 675 (1246788)   [3]
This is a Tes3 (345678)  
This is a Test 5 TS 544 Ord 12345
This is a Tes3 (PQ345678) 
This is a Test9 TS 544 Ord XP (A567843)
This is a test8  TS 344 Ord Z [9]

我想在圆括号中提取数字并将其放在变量中,所以我想要这个:

Var a1 =  The number within the round brackets so for e.g  1246788-5 or    345678 or PQ345678 or A567843
Var a2= I also want number in square brackets 2 or 3 or 9
Var a3 = I want to extarct all the numbers starting from TS and end at the next space so I need TS 544 ord 0, TS 544 Ord 675, TS 544 Ord 12345, TS 544 ORD XP

这些值来自文本文件。我可以使用正则表达式在C#或Vb.net中提取这些值吗。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

(?<=\()([^)]*)(?=\))|(?<=\[)([^\]]*)(?=\])|(?<=TS\s)(\d+\s+\S+\s+\d+\b)

您可以使用此单一正则表达式。group 1将是您的var a1,依此类推。请参阅演示。

https://regex101.com/r/cJ6zQ3/9