-4

I need to capture strings containing only one dot. String will mostly contains domain names like

test.com, fun.test.com, lesh.test.com.

I need to check only the first one and to ignore the string that has more than one dots.

How can I do this using regex?

Didi
  • 171
  • 1
  • 2
  • 13
  • 1
    Negating the match is faster i.e. No match `\..*\.` and string is ok. –  Dec 11 '19 at 22:20
  • 2
    What if the string contains no dots? – CAustin Dec 11 '19 at 22:37
  • 1
    For must contain a dot, use a string method to see if it contains a dot first, then validate no dup dots by negating the match i.e. no match `\..*\.` and string is ok. –  Dec 12 '19 at 17:21
  • 1
    The speed difference can be seen by given an equal sample of 2 passes and 2 fails out of 4 total `asssssssf.asssssssf \n assss.sssf.asssssssf \n asssssssf.asssssssf \n assss.sssf.asssssssf` the speed difference : `Regex1: \..*\. Completed iterations: 50 / 50 ( x 1000 ) Matches found per iteration: 2 Elapsed Time: 0.20 s, 202.78 ms, 202778 µs Matches per sec: 493,150 Regex2: ^[^.]+\.[^.]+$ Options: < m > Completed iterations: 50 / 50 ( x 1000 ) Matches found per iteration: 2 Elapsed Time: 0.37 s, 373.61 ms, 373605 µs Matches per sec: 267,662` –  Dec 12 '19 at 17:31

1 Answers1

0

Like this :

^[^.]+\.[^.]+$

Check explanations https://regex101.com/r/mn7Ccr/1

Gilles Quenot
  • 143,367
  • 32
  • 199
  • 195