2

I am new to 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.

The above pattern matches strings like 135.9.0 which will give output as 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.

6
  • Have a look at the tag info. Commented Oct 11, 2012 at 7:33
  • @user1194310... Do you also want to match: - 24.54.65 Or just single digits - 3.4.5? Commented Oct 11, 2012 at 7:42
  • 1
    @Anirudha.. You mean every one on SO should have improved English?? That is completely off-topic man.. If question is not clear to everyone, then probably that question is not Clear enough. And may be you have a brilliant English.. But you sure don't have ethics.. Which looks from the way you are talking.. Commented Oct 11, 2012 at 7:49
  • @RohitJain hmm..his example made it messy :) Commented Oct 11, 2012 at 7:51
  • @Anirudha.. And you were the only one who survived that mess :P.. But not completely.. Look at my comment on your Post.. Commented Oct 11, 2012 at 7:54

5 Answers 5

2

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 ..

Also note if you want to extract 5.9.0 from "string 5.9.0" then use the regex above. 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. 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Rereading the question, I think he does not want multiple digits in one place at all. So just remove the +s.
Hi shiplu, thanks for the answer. the above regex matches all the pattern which is separated by a period. Let me give you an example 5.0.0 From: 123.5.80<[email protected]> . The above code is matching [5.0.0,3.5.8,6.5.5,5.0.6]. That is not what I am looking for. I want to match only 5.0.0 in the above string. Let me know how can i do it?
@user1194310 you should include that in the question!
@user1194310 Regex can not match everything magically. You need to define first what should be matched and what not. Only then a regex can be tried to build. if not possible general parsing is more faster solution.
1

You can use either:

^\d\.\d.\d$

Or

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

1 Comment

Hi yossi, thanks for the answer. the above regex matches all the pattern which is separated by a period. Let me give you an example 5.0.0 From: 123.5.80<[email protected]> . The above code is matching [5.0.0,3.5.8,6.5.5,5.0.6]. That is not what I am looking for. I want to match only 5.0.0 in the above string. Let me know how can i do it?
1

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]

2 Comments

Read the question again. It says exactly three digits which is separated by a period(.) symbol.
I suggested to add a caret and a dollar sign to match the string to only the three digits.
1

This regular expression will do

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

\d matches a digit i.e 0-9

. need to be escaped with \ else it would match any character

7 Comments

This doesn't.. It will not match: - 24.56.2
@RohitJain you have not read the question..he want only 3 digits seperated by .
@Anirudha.. That was not my Downvote though.. But I didn't got OP question earlier.. I have edited my post.. :)
@Anirudha.. Your regex will not work in case of : - "Somestring containing 4.5.6 in middle".. It only matches at the end..
@RohitJain cuz he wanted so:the regex should match strictly
|
1

Below Regex will do what you want: -

\b\d+\.\d+\.\d+\b
  • \d matches any digit.

If you just want to match single digit separated by . you can use: -

\b\d\.\d\.\d\b
  • \b is for boundary checking..

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

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: -

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

12 Comments

Hi Rohit, thanks for your answer. I tried with the regex you gave but no luck. This is the string I am passing String ss = 5.0.0 From: 133.5.0<[email protected]>To:125.5.66. The output I am getting is [[5.0.0, 133.5, 125.5,136.5]. But i wanted it to be only 5.0.0. Since I am using java the regex am using is \\b[0-9].[0-9].[0-9]\\b and i tried with your regex also but not getting the proper output. Please let me know how can it be done.
thing is my string can contain three or four single digit which is separated by a period kind a pattern. So I am putting all the digits which matches my pattern into a List. Then i am processing according to my reqrment. So i want to match all the digits and put only which matches my pattern. Please let me know how to do it.
Yes you are right. That is what i want. I just want to store all the three digit combination like 5.3.2 3.4.3 5.8.7 into a list. The pattern should match exact three digit combination. If a pattern is like 123.3.3 or 12.44.44 or 123.456.66, my regex should not consider it.
Hi rohit, I would not be able to chat bcoz my company has blocked web chatting:(
@user1194310. Yeah it will match.. Use this pattern : -"[^\\d](\\d\\.\\d\\.\\d)[^\\d]"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.