Allow - (dash) in regular expression - asp.net

I have the following regular expression but I want the text box to allow the dash character
^[0-9a-zA-Z \/_?:.,\s]+$
Anyone know how I can do this?

The dash needs to be the first/last character in the character class in order to be used literally:
^[-0-9a-zA-Z \/_?:.,\s]+$
^[0-9a-zA-Z \/_?:.,\s-]+$
You could also escape it, if not the first/last:
^[0-9a-zA-Z\- \/_?:.,\s]+$

Simple answer, user \- in character class.
^[0-9a-zA-Z\- \/_?:.,\s]+$

Escape it with \ like:
^[\-0-9a-zA-Z \/_?:.,\s]+$

Related

Regular expression to allow dash sign

I currently need to allow a "-" sign in this regular expression ^[a-zA-Z0-9]*$.
You could use a regex like this:
^[a-z\d]+[-a-z\d]+[a-z\d]+$
Working demo
The idea is to use insensitive flag to avoid having A-Za-z and use only a-z. And also use \d that's the shortcut for 0-9.
So, basically the regex is compound of three parts:
^[a-z\d]+ ---> Start with alphanumeric characters
[-a-z\d]+ ---> can continue with alphanumeric characters or dashes
[a-z\d]+$ ---> End with alphanumeric characters
Simply add it as the first character after the opening bracket: ^[-a-zA-Z0-9]*$
Or, to match one or more of letters/numbers with a dash in between: ^[a-zA-Z0-9]+-[a-zA-Z0-9]+$
Hyphen can be included immediately after the open bracket [ or before the closing bracket
] in the character class. You should not include in the middle of the character class, otherwise it will treat as range characters and some Regex engine might not work also.
In your case both are valid solutions
(^[-a-zA-Z0-9]*$) - Starting of the Char class
(^[a-zA-Z0-9-]*$) - End of the Char class
Demo:
http://regex101.com/r/yP3sH7/2

RegEx for Client-Side Validation of FileUpload

I'm trying to create a RegEx Validator that checks the file extension in the FileUpload input against a list of allowed extensions (which are user specified). The following is as far as I have got, but I'm struggling with the syntax of the backward slash (\) that appears in the file path. Obviously the below is incorrect because it just escapes the (]) which causes an error. I would be really grateful for any help here. There seems to be a lot of examples out there, but none seem to work when I try them.
[a-zA-Z_-s0-9:\]+(.pdf|.PDF)$
To include a backslash in a character class, you need to use a specific escape sequence (\b):
[a-zA-Z_\s0-9:\b]+(\.pdf|\.PDF)$
Note that this might be a bit confusing, because outside of character classes, \b represents a word boundary. I also assumed, that -s was a typo and should have represented a white space. (otherwise it shouldn't compile, I think)
EDIT: You also need to escape the dots. Otherwise they will be meta character for any character but line breaks.
another EDIT: If you actually DO want to allow hyphens in filenames, you need to put the hyphen at the end of the character class. Like this:
[a-zA-Z_\s0-9:\b-]+(\.pdf|\.PDF)$
You probably want to use something like
[a-zA-Z_0-9\s:\\-]+\.[pP][dD][fF]$
which is same as
[\w\s:\\-]+\.[pP][dD][fF]$
because \w = [a-zA-Z0-9_]
Be sure character - to put as very first or very last item in the [...] list, otherwise it has special meaning for range or characters, such as a-z.
Also \ character has to be escaped by another slash, even inside of [...].

How to escape a RegEx that generates an error of “unexpected quantifier"on IE?

I use asp.net and C#. I have TextBox with an Validation Control with RegEx.
I use this code as validation.
ValidationExpression="^(?s)(.){4,128}$"
But only in IE9 I receive an error: unexpected quantifier from the javascript section.
Probably I have to escape my RegEx but I do not have any idea how to do it and what to escape.
Could you help me with a sample of code? Thanks
Write it like this instead :
^([\s\S]){4,128}$
I suspect that (?s) is the cause of the error.
Three problems: (1) JavaScript doesn't support inline modifiers like (?s), (2) there's no other way to pass modifiers in an ASP validator, and (3) neither of those facts matters, because JavaScript doesn't support single-line mode. Most people use [\s\S] to match anything-including-newlines in JavaScript regexes.
EDIT: Here's how it would look in your case:
ValidationExpression="^[\s\S]{4,128}$"
[\s\S] is a character class that matches any whitespace character (\s) or any character that's not a whitespace character--in other words, any character. The dot (.) metacharacter matches any character except a newline. Most regex flavors (like .NET's) support a "Singleline" or "DOTALL" mode that makes the dot match newlines, too, but not JavaScript.
JavaScript doesn't understand (?s) afaik, instead you can replace . with [^] or [\s\S].
Eg: ^[^]{4,128}$

Numeric textbox with regex

im trying to do a numeric textbox in asp.net using regex, and came up with:
^[^\s]+[/d]+[^\s]$
I want it to disallow leading/trailing whitespace, and allow only numbers.
Any clue why it doesnt work?
You can try this ^\d+$. \d matches digits. The one you wrote does not work because you are using /d instead of \d.
Since you want to disallow whitespace and other characters, why don't you try ^\d+$ and inverse the way of evaluation in your code?
Your regex currently means "anything but whitespace, followed by slashes and d-letters, followed by one more of anything but whitespace". A simple ^\d+$ is sufficient.

Regular Expression for ASP.NET ID Using Javascript

I am trying to extract the word "need" from this string.
ctl00_ctl00_ContentMainContainer_ContentColumn1__needDont_Panel1
I have tried [__]([.]?=Dont)
This is using javascript .match()
I have even tried to use http://gskinner.com/RegExr/ but just can't solve this one. Thanks for the help!
(?<=__)\w+(?=Dont)
Matches all alpha-numbers between __ and Dont
Edit
Sorry, I havent noticed word JavaScript. It does not support lookbehind, so __(\w+)(?=Dont) can be used there.
If Regex should match even when nothing comes between __ and Dont use "\w*" instead of "\w+". Be careful with ".*" because dot matches almost all characters, do you allow spaces in ID?
I haven't noticed
This will accomplish what you're looking for:
__(.*)(?=Dont)
You seem to be mixing up what a character class - square brackets [] - does, instead you should be using regular brackets ().
In your regex [__] will only match a single underscore _ and [.] will match a single period.
Your error is writing [__] instead of __ (without the braces). [__] matches only a single underscore, so it will match _ctl00_ContentMainContainer_ContentColumn1__need.
[.] is also wrong. You should use something like: [^_]+ (anything except underscore).

Resources