5

I need to capture strings containing more than one dot. String will mostly contains domain names like example.com, fun.example.com, test.funflys.com.

How can I do this using regex?

8
  • 5
    Have you tried anything yet? Commented Mar 5, 2014 at 14:01
  • wonder, how this question became off topic ( on close request ) Commented Mar 5, 2014 at 23:58
  • Probably because someone saw your question as a homework question and those are off-topic on SO. It can be said that you seem to be asking for someone to write some code for you. I'm not the one who voted to close, because I usually leave a comment and wait a bit before voting to close, but your question would be much better if it mentioned what you have tried and any problems you encountered in doing so. Also, questions about regex have to mention the language, because different languages have different implementations of regex. Commented Mar 6, 2014 at 5:24
  • @Jerry Aceepted, will correct it next time. Commented Mar 6, 2014 at 7:42
  • For an explanation, you can use this site where I put the regex you accepted. It generally is pretty clear, but if not, I could elaborate. Commented Mar 6, 2014 at 7:52

5 Answers 5

5

You should escape dot's because they have special meaning.

So, that regex would be;

.*\..*\..*

But you should be careful that \ is possibly have a special meaning on your programming language too, you may be have to escape them also.

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

1 Comment

This is a fairly straighforward pattern, it means that "A string(could be empty), followed by a dot, another string, followed by a dot and another string.". Simply I put two dots you want and modified them to match arbitrary strings between them.
5

This is with JavaScript.

var regex = /(\..*){2,}/;

regex.test("hello.world."); // true
regex.test("hello.world"); // false
regex.test("."); // false
regex.test(".."); // true

It searches for the pattern 'dot followed by anything (or nothing)', repeated 2 or more times.

Comments

2

Try this one :

(.*\.)+.*

or this one (to specifically match "characters" and just... anything) :

(\w*\.)+\w*

Demo :

http://regexr.com?38ed7

1 Comment

It matches, single dot too. I wanted to capture string which contains more than one dot...
0
^[A-Za-z0-9]*([A-Za-z0-9]*|\.+)+$

This will capture words or digits followed by at least one . and that can be repeated as many times.

Would be captured: hello.world.something hello......world.world something.09hello...hello

If you provide some more examples on what can and what can't be captured we can update this regex.

5 Comments

Have you tested? it's not working for the examples i mentioned in question
It works, but you probably tested on an entire list? Then you have to remove the ^ and $ (they imply begin and end of a sentence.)
Yet still I'm not entirely sure of what you are trying to achieve which makes it especially hard to provide you with the right regex.
In the above list, i need to match strings which contains two or more dots only. may be i'm confused you by giving strings of all variety...
It's about the context, are those strings in a file, are they on a single line, where do they come from and what have they in common, Most answers here capture about anything that contains at least one dot. That's why I said that my regex would also capture things you probably won't want to capture like hello....com and so on. Therefore the question what are you trying to capture and where did you get it from.
-1

Pattern.matches("[^\\.]+\\.[^\\.]+", "any_String")

  1. Gives 'false' if there are more than one dots in a String.
  2. Gives 'false' if there are dots in a starting or ending of the String.
  3. Gives 'false' for empty string String.

4 Comments

This is not answer to the question; OP wanted a regex that matches only for more than one dot; your answer would match a.b for example. OP also said nothing about leading or trailing dots, why do you think strings with those should be rejected?
@TamaMcGlinn This is majorly used for checking a file upload. To check if there are two or more file extensions or and empty file name etc.
for getting true for one or more dots we can use: Pattern.matches("[^\\.]*\\.[^\\.]*\\.[^\\.]*", "any_String")
okay, so this would be an answer to a different question; "What regex can I use for checking a filename before upload?". But it is not answer to the given question.

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.