Reguar expression to allow few Special Characters - regex

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.

Related

Regex: Match an Alphanumeric string that could contain the characther '-'

Eg: "_V9DXkFMCEeGrv54B-L8--A"
\w+ alone will not work
You can use:
[\w-]+
use this pattern: [-\w]+ \w is an alpanumeric character. Actually pattern depends from your language. For example in java you have to write [-\\w]+ and there also can be languages where - is a special character and you should escape it too. So please edit your question and add the language you use.

Regular expression to allow certain characters and disable a few others

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 (_).

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_])

regular expression generation

I need a regular expression to check a string should contain only letters and space.No other character other than letter [A-Z] and space are allowed.
Please help.
The complete regex looks like this
^[A-Z ]+$
You can simply create a character class and put the characters in that you want to allow:
[A-Z ]
if you want to allow also lower case letters then use
[A-Za-z ]
or use the i (IgnoreCase) option
So your character class matches 1 character. you want to repeat it to match more than one character.
+ would be at least one character, where
* would additionally match 0 characters
As last step you need to ensure that the complete string is matched, you can do this using anchors.
^ matches the beginning of the string
$ matches the end of the string (or a newline if you use the m (multiline) option
A character class should be sufficient
[A-Z ]+
i.e. one or more of letters between A-Z and space
Check that the string matches the following:
^[a-zA-Z ]*$
Regex character classes can be negated by putting a ^ symbol at the begining of them.
Your example could be negated like this: [^A-Z]. Add a space to allow the full range of characters you want to check for and you have [^A-Z ].
Now you have a validator that meets your criteria: If that regex returns true then your validation fails.
Since you didn't specify the programming language you're working in, I can't help you much further than that.
This will match what you need:
^[A-Z\s]+$
try matching with this regex
^[A-Za-z\s]+$
this should do the trick

Regex to match all of a set except certain ones

I'm sure this has been asked before, but I can't seem to find it (or know the proper wording to search for)
Basically I want a regex that matches all non-alphanumeric except hyphens. So basically match \W+ except exclude '-' I'm not sure how to exclude specific ones from a premade set.
\W is a shorthand for [^\w]. So:
[^\w-]+
A bit of background:
[…] defines a set
[^…] negates a set
Generally, every \v (smallcase) set is negated by a \V (uppercase) where V is any letter that defines a set.
for international characters, you may want to look into [[:alpha:]] and [[:alnum:]]
[^\w-]+
will do just that. Match any characters not in the \w set except hyphen.
You can use:
[^a-zA-Z0-9_-]
or
[^\w-]
to match a single non-hyphen, non-alphanumeric. To match one or more of then prefix with a +
In Java7 or above, you need to prepend the (?U) to match all locale specific characters. e.g.
(?U)[^\w-]
In a Java string (you need to escape \ character with another one):
(?U)[^\\w-]