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

本文关键字:字符串 提取 正则表达式 | 更新日期: 2023-09-27 18:16:06

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

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中使用正则表达式提取这些值吗?

如有任何帮助,不胜感激

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

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

你可以使用这个正则表达式。group 1就是你的var a1,以此类推。看到演示。

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