簡體   English   中英

使用正則表達式提取字符串

[英]extracting the string using regular expressions

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM