2017-08-12 44 views
-1

我想驗證文本文件中的每個字符串。每行中的內容由製表符分隔。模式檢查特定字符串是否必需

數據格式

EmployeeNumber {tab} EmployeeName {tab} Age {tab} IsCurrentlyEmployed 

Sample text: 

E001 {tab} Jim Watson {tab} 35 {tab} Yes 

E002 {tab} Mark Smith {tab} 50 {tab} No 

所以我猜的檢查應

(AlphaNumeric String){tab}(Normal String){tab}(Number){tab}(Yes/No) 

謝謝

+0

您提供的是正確的檢查,和你將能夠使用它作爲正則表達式進行驗證。你的問題到底是什麼?它是否會起作用?如果有更具體的正則表達式來驗證它?你在問什麼? – ChristianFigueroa

+0

我希望獲得有關RegEx表達式的幫助 –

回答

1

可以使用regex這樣,你有模式匹配提供:

\w+\t[a-zA-Z\s]+\t\d{1,3}\t(Yes|No) 

比賽:

E001 Jim Watson 35 Yes //where blanks are tabs. 

演練的步驟:

\w -> Matches any word character (equal to [a-zA-Z0-9_]) 
+ -> Matches between one and unlimited times, as many times as possible 
\t -> Matches a tab character 
[a-zA-Z\s]+ -> Matches entire alphabet, upper/lower case and spaces between one and unlimited times 
\t -> Matches a tab character 
\d{1,3} -> Matches any digit between 1-3 times (age 1-100+) 
\t -> Matches a tab character 
(Yes|No) -> Matches string "Yes" or "No" (it will break if string is "yes" or "no"). 
+0

謝謝您,步驟非常有幫助。 –

0

如果它需要你提供的確切格式,你可以使用

E\d+\t[A-Za-z] [A-Za-z]\t\d+\t(?:Yes|No) 

regex將匹配以下

E015 {Tab} John Doe {Tab} 32 {Tab} Yes 
E451 {Tab} Jane Doe {Tab} 25 {Tab} No 

但不是以下

015 {Tab} John Doe {Tab} 32 {Tab} Yes # the E at the beginning is missing 
E451 {Tab} Jane {Tab} 32 {Tab} No # only the first name is given 
E {Tab} Johnson Doe {Tab} 48 {Tab} Yes # no employee number provided 

如果你正在尋找一個較爲寬鬆的正則表達式,使用所提供的一個demogorgon.net的回答

+0

謝謝你解釋。 –

相關問題