简体   繁体   English

需要正则表达式来匹配字符串

[英]Need Regex to match a String

I am new to Java. 我是Java新手。 I need a regex to match string with such a pattern: 我需要一个正则表达式来匹配具有这种模式的字符串:

[0-9].[0-9].[0-9]

For example, the regex should match strictly 5.5.0 or 4.6.5 or 5.2.2 . 例如,正则表达式应严格匹配5.5.04.6.55.2.2

The above pattern matches strings like 135.9.0 which will give output as 5.9.0 上面的模式匹配像135.9.0这样的字符串,它将输出为5.9.0

But I do not want to match such kind of pattern. 但是我不想匹配这种模式。

I should be able to match exactly three digits which is separated by a period(.) symbol. 我应该能够完全匹配由句点(。)符号分隔的三个数字。

As . 作为. is a meta-character in regular expression, escape it like \\. 是正则表达式中的元字符,请像\\.一样将其转义\\. . Hence the expression, 因此表达

[0-9]\.[0-9]\.[0-9]

This can be simplified to 这可以简化为

\d\.\d\.\d

or even to 甚至

(\d\.){2}\d

This will match any 3 digits separated by a . 这将匹配以分隔的任意3位数字. .

Also note if you want to extract 5.9.0 from "string 5.9.0" then use the regex above. 另请注意,如果要从"string 5.9.0"提取5.9.0 ,请使用上面的正则表达式。 But if you want to match 5.9.0 from exactly "5.9.0" (the whole string, used in some kind of validation) they you should use ^ at the start and $ at the end. 但是,如果要从"5.9.0" (整个字符串,用于某种验证)中完全匹配5.9.0 ,则应在开头使用^ ,在末尾使用$ So it'll be something like 所以它会像

^(\\d\\.){2}\\d$

Lastly (as your question is quite unclear whether you want to match single digit or a sequence of digits), to match sequence of digit use [0-9]+ instead of [0-9] or \\d+ instead of \\d . 最后(由于您的问题非常不清楚,是要匹配一位数字还是要匹配数字序列),要匹配数字序列,请使用[0-9]+而不是[0-9]\\d+而不是\\d

You should escape your dots with a backslash since a dot matches every character in regular expressions. 由于点与正则表达式中的每个字符都匹配,因此应使用反斜杠转义点。
You should add a caret (^) at the beginning and a dollar sign at the end of the regex to limit the matching to the entire string and not part of it (which may match if the string has things you do not want). 您应在正则表达式的开头添加一个脱字符号(^),在正则表达式的末尾添加一个美元符号,以将匹配项限制为整个字符串而不是整个字符串的一部分(如果字符串包含您不想要的内容,则可能会匹配)。

^[0-9]\.[0-9]\.[0-9]$

Or match a predefined prefix/suffix, or a non number in the beginning by using (this is only if you the string may have something other than a number in the beginning, otherwise use the above): 或通过使用匹配预定义的前缀/后缀或开头的非数字(仅当您的字符串开头可能有数字以外的其他字符时,否则使用以上命令):

[^0-9]

This regular expression will do 这个正则表达式可以

\b\d\.\d\.\d\b

\\d matches a digit ie 0-9 \\d匹配一个数字,即0-9

. need to be escaped with \\ else it would match any character 需要使用\\进行转义,否则它将与任何字符匹配

Below Regex will do what you want: - 正则表达式下面将执行您想要的操作:-

\b\d+\.\d+\.\d+\b
  • \\d matches any digit. \\d匹配任何数字。

If you just want to match single digit separated by . 如果你只是想匹配single相隔 . you can use: - 您可以使用: -

\b\d\.\d\.\d\b
  • \\b is for boundary checking.. \\b用于边界检查。

Try this code: - 尝试以下代码:-

String str = "5.0.0 abc 5.6.7 abc 12.45.65 From: 133.5.0<pos1.002otak.com>";

Pattern pattern = Pattern.compile("\\b\\d\\.\\d\\.\\d\\b"); 
Matcher match = pattern.matcher(str);

while (match.find()) {
    System.out.println(match.group(0));
}

OUTPUT OUTPUT

5.0.0
5.6.7
// 12.45.65 is not printed

UPDATE : - 更新 :-

To match 1.4.5 inside abc1.4.5def and not in 121.4.546 use the below pattern: - 要在abc1.4.5def而不是121.4.546匹配1.4.5121.4.546使用以下模式:-

[^\\d](\\d\\.\\d\\.\\d)[^\\d]

You can use either: 你可以使用:

^\d\.\d.\d$

Or 要么

\b\d\.\d.\d\b

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

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