简体   繁体   English

使用正则表达式验证冒号分隔的输入

[英]Using regular expression to validate colon separated inputs

I'm reading in a file for a Java application which has data separated by colons in the format:我正在读取一个 Java 应用程序的文件,该文件的数据由以下格式的冒号分隔:

test : test : 0 : 0

Where the first two segments are names of something and the last two are digits.其中前两段是某物的名称,后两段是数字。

The match should fail if the input is not formatted in that exact way above (aside from the data being different)如果输入的格式不是上面那样的格式(除了数据不同),匹配应该失败

test : test : 0 : 0 -----> pass
: test: 0 : 0       -----> fail
0 : test : 0 : test -----> fail
test test : 0 : 0   -----> fail

So the match will fail if there are any segments omitted, if the digits and words do not appear where they should, ie word : word : digit : digit , and there has to be 3 colons and 4 segments no more no less as above.因此,如果省略了任何段,则匹配将失败,如果数字和单词没有出现在它们应该出现的位置,即word : word : digit : digit ,并且必须有 3 个冒号和 4 个段,不能超过以上。

This is where I have gotten so far but it's not quite right:这是我到目前为止所取得的进展,但并不完全正确:

^\D+(?:\s\:\s\w+)*$

You may use a regex like你可以使用像这样的正则表达式

^[a-zA-Z]+\s*:\s*[a-zA-Z]+(?:\s*:\s*\d+){2}$

Details细节

  • ^ - start of string (implicit in String#matches ) ^ - 字符串的开始(隐含在String#matches
  • [a-zA-Z]+ - 1+ ASCII letters [a-zA-Z]+ - 1+ ASCII 字母
  • \\s*:\\s* - a : enclosed with 0+ whitespaces \\s*:\\s* - a :用 0+ 个空格括起来
  • [a-zA-Z]+ - 1+ ASCII letters [a-zA-Z]+ - 1+ ASCII 字母
  • (?:\\s*:\\s*\\d+){2} - two occurrences of : enclosed with 0+ whitespaces and then 1+ digits (?:\\s*:\\s*\\d+){2} - 出现两次:用 0+ 个空格和 1+ 个数字括起来
  • $ - end of string (implicit in String#matches ) $ - 字符串结尾(隐含在String#matches

NOTE : If there must be an obligatory single space between the items, you need to replace \\s* with \\s .注意:如果项目之间必须有一个强制性的单个空格,则需要将\\s*替换为\\s To match 1 or more whitespaces, \\s* must be turned into \\s+ .要匹配 1 个或多个空格,必须将\\s*转换为\\s+

In Java, you may write it as在 Java 中,你可以把它写成

s.matches("[a-zA-Z]+\\s*:\\s*[a-zA-Z]+(?:\\s*:\\s*\\d+){2}")

See the regex demo查看正则表达式演示

I would just use String#matches on each line, with the following pattern:我只会在每一行上使用String#matches ,使用以下模式:

[a-z]+ : [a-z]+ : [0-9]+ : [0-9]+

For example:例如:

String line = "test : test : 0 : 0";
if (line.matches("[a-z]+ : [a-z]+ : [0-9]+ : [0-9]+")) {
    System.out.println("Found a match");
}

Here you go (demo at Regex101 ):给你(在Regex101演示):

[a-zA-Z]+\s+:\s+[a-zA-Z]+\s+:\s+\d+\s+:\s+\d+

Explanation:解释:

  • [a-zA-Z]+ stands for 1 or more letters ( + is the modifiers allowing to match the previous statement at least once [a-zA-Z]+代表 1 个或多个字母( +是修饰符,允许至少匹配上[a-zA-Z]+语句一次
  • \\s+ stands for 1 or more \\s+代表 1 个或多个
  • : is the : character, literally ::字符,字面意思
  • \\d+ stands for at least one digit (remove the + to match one digit exactly) \\d+代表至少一位数字(去掉+以精确匹配一位数字)

Finally, compose those parts according to your needs.最后,根据您的需要组合这些部分。 You might want to make the Regex make stricter replacing the \\s+ with only one empty space你可能想让正则表达式更严格地用一个空格替换\\s+ . .

Validate the String using the method String::matches (don't forget to use two slashes \\\\ ):使用String::matches方法验证字符串(不要忘记使用两个斜杠\\\\ ):

boolean isValid = string.matches("[a-zA-Z]+\\s+:\\s+[a-zA-Z]+\\s+:\\s+\\d+\\s+:\\s+\\d+");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM