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.

Recommended Answers

All 4 Replies

    ^(?!.*\.{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.

ok thanks Martyn, i'll give it a try

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.