0

im having a hardtime with this regex, i need to validate two words with a required point between them, no special characters, no @, no spaces, just the words and the point in the middle. for example:

test.test 

i have been trying with this one, however it doesnt work:

^[a-z']*.[a-z']+$

can someone please help me with this? i have been stucked for a while now, thanks in advance

Solar Confinement
  • 2,153
  • 3
  • 21
  • 28
  • I wouldn't define `test.test` as a "two letters string". Do you want a string with letter.letter (`a.a`) or word.word (`something.something`). – Balastrong Jun 19 '20 at 16:48
  • @Balastrong yes thats exactly what i need, sorry...! – Solar Confinement Jun 19 '20 at 16:49
  • 3
    `/^[a-z']+\.[a-z']+$/` – anubhava Jun 19 '20 at 16:50
  • 1
    thats exactly what i needed, thanks!!! how can i vote the right answer? @anubhava – Solar Confinement Jun 19 '20 at 16:52
  • 1
    Depending on where you are running your regex you need to escape the right characters. You certainly need to escape the dot. If you are running on a linux shell (e.g.: `echo "aaa" | grep "a\+"`), with many basic commands you have to escape the + character as well. Some later or advanced commands (like egrep) don't need that. Be sure you figure out what needs to be escaped in your environment. – Uluaiv Jun 19 '20 at 16:56
  • 1
    You may accept answer by @dang as he was the first one to post an answer. – anubhava Jun 19 '20 at 16:59

2 Answers2

2

The dot is the wildcard operator, it must be escaped with a backslash, \.

This Wiki on Regex contains info about the wildcard and other common characters for RegEx under "Basic Concepts"

JvdV
  • 70,606
  • 8
  • 39
  • 70
dang
  • 118
  • 1
  • 10
2

Escape the dot with slash to make it literal dot otherwise it will match any character

^[a-z']+\.[a-z']+$

In regular expressions, the dot (.) is a special character used to match any one character.

Use the backslash to escape any special character and interpret it literally; for example:

\\ (escapes the backslash)
\[ (escapes the bracket)
\{ (escapes the curly brace)
\. (escapes the dot)
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103