Reputation: 563
I'm having a hard time on a regex expression.
It's only requirement is that if there is a dot (.) in the word, there must be a letter on either side of the dot. There can be any number of dots in the word and any number of letters in between the dots. There just has to be a letter on either side of a dot.
I have the mostly figured it out but I am having issue with dots that are only separated by one letter (see example below)
Currently I have this expression:
^(\s*[0-9A-Za-z]{1,}[.]{0,1}[0-9A-Za-z]{1,}\s*)+$
this works for the following:
However, this does not work for words if the dots are only seperated by one letter, as follows:
Anyone know how I could solve this?
EDIT:
BHustus solution below is the better solution.
However, I did take what BHustus has shown and combined it with what I had before to come up with a less "confusing" pattern just in case anyone else was interested.
^(\s*[\d\w]+([.]?[\d\w]+)+\s*)+$
The key was to have the . and the 1 word after be in its own group and repeat. ([.]?[\d\w]+)+
Thanks.
Upvotes: 1
Views: 11169
Reputation: 8208
Do you have to use a Regex? The accepted answer's Regex is pretty difficult to read. How about a simple loop?
for(int i = 0; i < str.length; i++)
{
char ch = str[i];
if(ch == '.')
{
if(i == 0) return false; //no dots at start of string
if(i == str.length - 1) return false; //no dots at end of string
if(str[i + 1] == '.') return false; //no consecutive dots
}
else if(!IsLetter(ch) && !IsNumber(ch))
{
return false; //allow only letters and numbers
}
}
return true;
Upvotes: 0
Reputation: 3581
([\w]+\.)+[\w]+(?=[\s]|$)
To explain:
The first group, in the parentheses, matches 1 or more letter or number (\w
is shorthand for [A-Za-z0-9]
and +
means "match the preceding one or more times", shorthand for {1,}
), followed by one period. After it has matched one or more cycles of [\w]+\.
, the final [\w]+
ensures that there is at least one letter at the end and consumes all characters until it reaches a non-character. finally, the (?=[\s]|$)
is a lookahead assertion that ensures there is either whitespace immediately ahead ([\s]
), or the end of the string ($
) (with |
being the regex "OR" character). If the lookahead fails, it doesn't match.
Online demo, showing all your test cases
Upvotes: 5