-1

I am working with a Regex to validate a string with the following requeriments:

  1. It does not start with special characters.
  2. It can countains numbers, letters and special characters, with the exception of periods and parentheses.
  3. Special characters can appear 1 time only.

The Regex i have is this one:

/^[a-zA-Z0-9]+(?!\S*([\W_.()])\S*\1{1,})\S*$/

The regex is not working fine, because it continues accepting periods and parentheses.

Would you know how can achieve the above requeriments?

Thanks in Advance.

4
  • 2
    You need to define what special characters are. Commented Oct 9, 2024 at 13:56
  • Also think of whitespace, would you consider space/tab as special character? I guess not when looking at your current pattern. Try something like this. Commented Oct 9, 2024 at 13:59
  • 1
    Thanks for responding. space/tab are not allowed. I did not include the special characters, because this is subjetive in each application, there are validations in other level what define the allow characters. It is the reason because i wanted doing global with \W. Commented Oct 9, 2024 at 14:05
  • This sounds like you're trying to validate a password against a set of rules. I suggest that you not try to do this with a single regex. Write a function that calls multiple regexes. It will be much simpler to understand and to modify in the future. Commented Oct 9, 2024 at 16:14

1 Answer 1

3

I think you don't even need a lookahead to check for one special character. If you define those as [not whitespace, no digit or letter, no dot, no parentheses] that would be [^\s\da-z.)(].

^[^\W_]+[^\s\da-z.)(]?[^\W_]*$

See this demo at regex101 - Use with i-flag (prepend (?i) if inline flags are supported).
[^\W_] is a short for alphanumeric character, one or more at start and any amount at end.


Or if you want to allow multiple special characters but none more than once with lookahead:

^[^\W_]+(?:([^\s\da-z.)(])(?!.*\1)[^\W_]*)*$

Demo at regex101 - The special character is captured to the first group, checked if \1 reoccurs. Using a non-capture group for repetition of specials followed by any amount of alphanumeric.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reponse Bobble, this looks cleaner the mine. Just on question, I am testing it, but when one special character is next to another, or there are more than one, the validation left to work, for example: ACEEL$!0A101 or Example*L% is there one way to avoid this one?
@J.Abel Both your examples are not supposed to validate because they contain more than 1 special characters see demo. Isn't that what you wanted.
@J.Abel I think I first misunderstood. You want to allow multiples special characters, but none reoccuring, right? Please reread my updated answer, here is the regex101 demo.
Yes, this approach is working for me. Thanks so much @Bobble bubble. This solves my requirement. :
@J.Abel Welcome! Glad that helped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.