2017-08-12 105 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

谢谢你解释。 –

相关问题