Regular expression to allow certain characters and disable a few others - regex

I have to write a regular expression which allows alphabets and numbers, but does not permit characters like / and ? . Can someone help me out on this? Thanks in advance

This regex does what you have asked for:
^(?!.*(/|\?))[a-zA-Z0-9]+$
It simply matches only on input that is comprised solely of letters and/or numbers, plus it does a negative lookahead to make sure no / or ? are present.
To exclude more special chars, just add them to the look ahead, for example to also exclude # use this (?!.*(/|\?|#)) (the ? needs escaping with a backslash \)

Assuming you want to allow more than just ASCII letters and numbers (and your regex flavor supports Unicode), you can use
^[\p{L}\p{N}]*$

you could try to concentrate on characters that you dont want to allow if that list is more clear than allowed one:
^[^\?\/\s\(\)]+$
Fill [] with more characters that you dont like

The meta character \w matches alphanumerics which means all numbers, ASCII letters and underscore (_).

Related

Why does below regex not work in vim? (Specifically, the ? in the regex)

[0-9]* (-)?[0-9]* q
This regex does not seem to work in vim for below text
34778965 -1 q some text here
[SOLVED]
Thank you! I realize it all has to be escaped. What felt inconsistent is some needed to be escaped like \? but some not, like * or $. vimregex.com helped.
You can use \d to denote numbers in vim regex.
A good pattern for your text would be
\d\+ -\? \d\+ q
In general, vim assumes that the characters as their literal ones. So, if you give \d+, it would be understood as any digit follows by a plus sign. So, you will have to escape such regex specific characters in patterns.
In Vim, you need to escape some common regex special characters for them to act as special operators. E.g. (-) group must be written in a non very magic mode as \(-\). In a very magic mode, your pattern would work as is - :%s/\v[0-9]* (-)?[0-9]* q/replace/g
In your case, you just do not need the grouping at all because you quantify one single hyphen inside parentheses, so they can be removed:
[0-9]* -\? [0-9]* q

regular expression for match 1 alphabet, 1 digit and allow all special characters

In my website(PHP, Mysql), I am using the below regular expression for validate password
^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]{6,}$
I got this from http://regexlib.com/. This validate minimum 6 characters which should have atleast one alphabet and one numeric character. It will not allow any special characters
I need to allow all the special characters also.
How could I modify this? I am not familiar with regular expressions. Please help
Simply add the special characters you want to allow in the character class:
^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z.!&/()=?+*~#'_:.,;-]{6,}$
^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]{6,}$
You just have to add special characters to your existing character class.
Why use lookahead ?
\A[0-9a-zA-Z.!&/()=?+*~#'_:.,;-]{6,}\z

Reguar expression to allow few Special Characters

I am new to Validation through RegEx
I want to Validate an input field through regex that
Must have Alphanumeric Characters
Must contain - _ / . ( )
In this case your regex will define a set (you need to escape some special characters with \):
^[a-zA-Z0-9\-_\/\.\(\)]*$
This one will do the job:
^[\w/.()-]+$
\w means [a-zA-Z0-9_]
For alpha numeric characters (assuming English characters), you can use the following: ^[A-Za-z0-9_\/.()-]+$.
Please take a look at this tutorial for more information and here for a more detailed explanation of the regex.

RegEx :- Need to write a regex which should not allow digits

I need to write a regular expression which should not allow any digits. it should allow any other characters except digits. I tried expression like :- ~[0-9]+
but it restricts everything. could you pls help me?
It is not clear what flavor of regex you need, but in the general, one of the following should work:
^[^0-9]*$
^[^\d]*$
^\D*$
^[[:^digit:]]*$
^\P{IsDigit}*$
The last two forms will work with Unicode digits.
The atom [^0-9] matches anything but a digit; to make sure that in the whole string there are no digits, I added the markers of string start (^) and end ($).
If you want to match any part of a string that contains at least one character that is not a digit, replace the ^...*$ part of the regex by ...+:
[^0-9]+
\D+
etc.
Try [^0-9]+. Note that this will only prevent ASCII digits from appearing, not unicode ones.

Regex help NOT a-z or 0-9

I need a regex to find all chars that are NOT a-z or 0-9
I don't know the syntax for the NOT operator in regex.
I want the regex to be NOT [a-z, A-Z, 0-9].
Thanks in advance!
It's ^. Your regex should use [^a-zA-Z0-9]. Beware: this character class may have unexpected behavior with non-ascii locales. For instance, this would match é.
Edited
If the regexes are perl-compatible (PCRE), you can use \s to match all whitespace. This expands to include spaces and other whitespace characters. If they're posix-compatible, use [:space:] character class (like so: [^a-zA-Z0-9[:space:]]). I would recommend using [:alnum:] instead of a-zA-Z0-9.
If you want to match the end of a line, you should include a $ at the end. Turning on multiline mode is only when your match should extend across multiple lines, and it reduces performance for larger files since more must be read into memory.
Why don't you include a copy of sample input, the text you want to match, and the program you are using to do so?
It's pretty simple; you just add ^ at the beginning of a character set to negate that character set.
For example, the following pattern will match everything that's not in that character set -- i.e., not a lowercase ASCII character or a digit:
[^a-z0-9]
As a side note, some of the more helpful Regular Expression resources I've found have been this site and this cheat sheet (C# specific).
Put at ^ at the begining of your character class expression: [^a-z0-9]
At start [^a-zA-Z0-9]
for condition;
pre_match();
pre_replace();
ergi();
try this
You can also use \W it's a shorthand for non-word character (equal to [^a-zA-Z0-9_])