简体   繁体   English

正则表达式匹配特定模式

[英]Regex match on specific pattern

Definitely not an expert at Regex so I'm struggling here a bit. 肯定不是Regex的专家,所以我在这里有点挣扎。 I'd like to create a pattern that will match the following format: 我想创建一个与以下格式匹配的模式:

cat:hairy, rat:hairless               [correct]

cat:hairy, rat:hairless, dog:fat      [correct]

cat:hairy,      rat:hairless          [correct]



cat                                   [incorrect]

cat, dog                              [incorrect]

cat:hairy,                            [incorrect]

cat:hairy, monkey, dog:fat            [incorrect]

cat:hairy rat:hairless, dog:fat       [incorrect]

So far the regex I have is as follows: 到目前为止,我拥有的正则表达式如下:

((\S):(\S))

However, this does not work in all cases. 但是,这并非在所有情况下都有效。 Any help would be appreciated! 任何帮助,将不胜感激!

Update Language : Java Format : the xxx:yyy can be any letter or number, upper or lower case 更新 语言 :Java 格式 :xxx:yyy可以是任何字母或数字,大写或小写

You may use the following regex: 您可以使用以下正则表达式:

^[a-zA-Z0-9]+:[a-zA-Z0-9]+(?:,\s+[a-zA-Z0-9]+:[a-zA-Z0-9]+)+$

See the regex demo 正则表达式演示

Details : 详细资料

  • ^ - start of string ^ -字符串的开头
  • [a-zA-Z0-9]+ - 1 or more alphanumerical chars [a-zA-Z0-9]+ -1个或更多字母数字字符
  • : - a colon : -冒号
  • [a-zA-Z0-9]+ - 1 or more alphanumerical chars [a-zA-Z0-9]+ -1个或更多字母数字字符
  • (?:,\\s+[a-zA-Z0-9]+:[a-zA-Z0-9]+)+ - 1 or more sequences of (?:,\\s+[a-zA-Z0-9]+:[a-zA-Z0-9]+)+ -1个或多个序列
    • , - comma , -逗号
    • \\s+ - 1 or more whitespaces \\s+ -1个或多个空格
    • [a-zA-Z0-9]+ - 1 or more alphanumerical chars [a-zA-Z0-9]+ -1个或更多字母数字字符
    • : - a colon : -冒号
    • [a-zA-Z0-9]+ - 1 or more alphanumerical chars [a-zA-Z0-9]+ -1个或更多字母数字字符
  • $ - end of string $ -字符串结尾

Depending on the regex flavor and purpose, [a-zA-Z0-9] may be replaced with [[:alnum:]] (POSIX, Ruby, PCRE) or \\p{Alnum} (Java). 根据正则表达式的风格和用途,可以将[a-zA-Z0-9]替换为[[:alnum:]] (POSIX,Ruby,PCRE)或\\p{Alnum} (Java)。 So, in Java, you'd use 因此,在Java中,您将使用

.matches("\\p{Alnum}+:\\p{Alnum}+(?:,\\s+\\p{Alnum}+:\\p{Alnum}+)+")

Note there is no need for ^ and $ as .matches() anchors the pattern by default. 注意,由于.matches()默认锚定模式,因此不需要^$

There is one scenario that is not listed as correct or incorrect, but implied correct by your first regex ((\\S):(\\S)) : a singleton cat:hairy . 有一种情况没有被列为正确或不正确,但是第一个正则表达式((\\S):(\\S))暗示正确的情况是:singleton cat:hairy In this case, then Wiktor's excellent solution would be terminated with *$ instead of +$ . 在这种情况下,Wiktor的出色解决方案将以*$而不是+$终止。

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

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