[英]Need regex to match the given string
I need a regex to match a particular string, say 1.4.5
in the below string . 我需要一个正则表达式来匹配特定的字符串,在下面的字符串中说1.4.5
。 My string will be like 我的弦会像
absdfsdfsdfc1.4.5kdecsdfsdff
I have a regex which is giving [c1.4.5k]
as an output. 我有一个正则表达式给出[c1.4.5k]
作为输出。 But I want to match only 1.4.5
. 但是我只想匹配1.4.5
。 I have tried this pattern: 我已经尝试过这种模式:
[^\\W](\\d\\.\\d\\.\\d)[^\\d]
But no luck. 但是没有运气。 I am using Java. 我正在使用Java。 Please let me know the pattern. 请让我知道模式。
When I read your expression [^\\\\W](\\\\d\\\\.\\\\d\\\\.\\\\d)[^\\\\d]
correctly, then you want a word character before and not a digit ahead. 当我正确阅读您的表达式[^\\\\W](\\\\d\\\\.\\\\d\\\\.\\\\d)[^\\\\d]
时,您想要的是前面的文字字符而不是前面的数字。 Is that correct? 那是对的吗?
For that you can use lookbehind and lookahead assertions . 为此,您可以使用lookbehind和lookahead断言 。 Those assertions do only check their condition, but they do not match, therefore that stuff is not included in the result. 这些断言仅检查其条件,但不匹配,因此结果中不包含任何内容。
(?<=\\w)(\\d\\.\\d\\.\\d)(?!\\d)
Because of that, you can remove the capturing group. 因此,您可以删除捕获组。 You are also repeating yourself in the pattern, you can simplify that, too: 您还在模式中重复自己,也可以简化它:
(?<=\\w)\\d(?:\\.\\d){2}(?!\\d)
Would be my pattern for that. 这将是我的模式。 (The ?:
is a non capturing group) ( ?:
是一个非捕获组)
我认为这应该做到: ([0-9]+\\\\.?)+
Regular Expression 正则表达式
((?<!\d)\d(?:\.\d(?!\d))+)
As a Java string: 作为Java字符串:
"((?<!\\d)\\d(?:\\.\\d(?!\\d))+)"
Your requirements are vague. 您的要求含糊不清。 Do you need to match a series of exactly 3 numbers with exactly two dots? 您是否需要将三个正整数与两个正点匹配?
[0-9]+\.[0-9]+\.[0-9]+
Which could be written as 可以写成
([0-9]+\.){2}[0-9]+
Do you need to match x many cases of a number, seperated by x-1 dots in between? 您是否需要匹配x个数字的许多情况,中间用x-1个点分隔?
([0-9]+\.)+[0-9]+
Use look ahead and look behind. 使用向前看和向后看。
(?<=c)[\d\.]+(?=k)
Where c is the character that would be immediately before the 1.4.5 and k is the character immediately after 1.4.5. 其中c是紧接1.4.5之前的字符,k是紧接1.4.5之后的字符。 You can replace c and k with any regular expression that would suit your purposes 您可以使用任何适合您目的的正则表达式替换c和k
String str= "absdfsdfsdfc**1.4.5**kdec456456.567sdfsdff22.33.55ffkidhfuh122.33.44";
String regex ="[0-9]{1}\\.[0-9]{1}\\.[0-9]{1}";
Matcher matcher = Pattern.compile( regex ).matcher( str);
if (matcher.find())
{
String year = matcher.group(0);
System.out.println(year);
}
else
{
System.out.println("no match found");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.