Kev1n91
Kev1n91

Reputation: 3673

Why does the "." get not caught in the regex?

I want to have a regular epxresion, that allows that checks wether the email adress given is correct. Firstly, it will check if a specific provider is there, in this case (@test.de) - this is not the problem. However the email names that are allowed must consist only of letters or dots. so: [email protected] is valid. However this specific case does not get accepted. My regex looks like the following:

[A-Za-z\.]{1,}\b@test\.de\b

It works fine, for all other cases but if a "." is only in front of the @it does not fit.

Any pointers what I am doing wrong?

Upvotes: 1

Views: 64

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

The first word boundary \b in your pattern requires that there must be a word char before @. Thus, a dot cannot appear there, the match is failed.

You need to remove the word boundary, use

[A-Za-z.]+@test\.de\b

Note you do not need to escape a dot inside a character class, it already denotes a literal dot.

If you still want to match "whole" words after removing \b, you might use lookbehinds (if the regex engine supports them):

(?<!\w)[A-Za-z.]+@test\.de\b

or to only match after whitespace/start of string:

(?<!\S)[A-Za-z.]+@test\.de\b

Or just use a word boundary if the name starts with a letter, and a non-word boundary if it starts with a dot:

(?:\b[A-Za-z]|\B\.)[A-Za-z.]*@test\.de\b

See this demo

Upvotes: 2

Related Questions