0

I am trying to write a regular expression with the following requirements:
(1) not allow #
(2) not allow &
(3) not allow \t (tab)
(3) not allow multiple spaces (single space is ok)

Here is I tried:

^[^#&\t\s+]*$

However, I can't get my output as I want. What am I doing wrong?
Can someone help me?

KKL Michael
  • 795
  • 1
  • 13
  • 31
  • 2
    Do you have some examples as well? In which language are you trying this? – AKS May 17 '16 at 05:01
  • 2
    I'm guessing based on your tag history that this is in PHP or JavaScript? Different languages have different flavors (versions) of regex, so that is important to the question. – 4castle May 17 '16 at 05:06
  • Your regex works fine on [www.regex101.com](https://regex101.com/r/kT1oK0/1). – Tim Biegeleisen May 17 '16 at 05:08
  • 2
    When you say "single space is ok" does that refer to one space in the entire string, or to only having non-spaces adjacent to every space? – 4castle May 17 '16 at 05:08
  • http://stackoverflow.com/questions/37018848/how-to-block-entering-gmail-adress-in-textbox-or-show-popup – akhil viswam May 17 '16 at 05:09
  • It's easier to construct a regex that *matches* the forbidden conditions, and reject it when it matches – kennytm May 17 '16 at 05:13

2 Answers2

0

This will work in many regex flavors (including PCRE and JavaScript):

^(?!.*  )[^#&\t]*$

First, do a negative look-ahead to make sure there aren't any double spaces, and then match zero or more characters that aren't #, &, or \t.

Regex101 Tested

4castle
  • 32,613
  • 11
  • 69
  • 106
0

Break the task up into a basic expression for characters that are allowed, plus a negative look ahead anchored to start for the "no double spaces" assertion:

^(?!.*  )[^#&\t]*$
rock321987
  • 10,942
  • 1
  • 30
  • 43
Bohemian
  • 412,405
  • 93
  • 575
  • 722