Hello All,

I am new to writing regular expressions and have been trying to write a regex for the following criteria:

Cannot contain ~, #, &, {}, asterisk, {}, \, :, <>, ?, /, +, |, or "
It cannot start with a period or an underscore
Cannot have two consecutive periods
Cannot end in a period

This is what I have thus far:

 '^(?!.*\.{2})^[a-zA-Z0-9][a-zA-Z0-9_. ]+[^.]$'

This regex, however, does not produce the desired effects. When removing the appended "[^.]" the regex works as desired other than the fact that it doesn't validate strings ending with a period. Please help me solve this issue of validating strings that end in a period.

Thanks for all your time and help.

4 Years
Discussion Span
Last Post by while(!success)
    ^(?!.*\.{2})[a-zA-Z0-9][a-zA-Z0-9_. ]+(?!\.)$

I'm not sure if this is what you require, but you weren't using a negative lookahead to check anything that "didn't" end with a period. You were looking for a positive match for anything that wasn't a period.

Edited by Martyn_86


unfortunately that regex didn't work Martyn. When running the regex through rubular.com, when ending the string with a "." it still comes up as a match.


I am trying this now:


which almost works, except when I enter an illegal character it creates a match until I enter a character after the illegal character to create a non match.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.