Regex 需要对特定字符串进行模式检查
我想验证文本文件中的每个字符串。每行中的内容由制表符分隔 数据格式:Regex 需要对特定字符串进行模式检查,regex,Regex,我想验证文本文件中的每个字符串。每行中的内容由制表符分隔 数据格式: 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 Str
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)
谢谢您可以使用与您提供的模式相匹配的
正则表达式
:
\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").
如果需要使用您提供的格式,您可以使用
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
但不是下面的
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
的答案中提供的正则表达式,您提供的检查将是正确的,您可以将其用作正则表达式进行验证。你的问题到底是什么?它是否有效?是否有更具体的正则表达式来验证它?你在问什么?我想要得到关于确切正则表达式的帮助谢谢,走查步骤非常有用。谢谢你的解释。