I'm looking for a regex that will allow Alpha Numeric and most all special characters except white space. It should be usable in c#. It would be nice if .net supported posix style but I can't seem to get it to work. TIA
Pretty sure \S (note capitalization) is the non-whitespace character class.
Something along the lines of: [^\s]+ should do the trick.
This roughly translates as "match one or more consecutive characters that are not whitespace" (\s matches a space, tab, or line break).
Related
I've a textbox in an ASP.NET application, for which I need to use a regular expression to validate the user input string. Requirements for regex are -
It should allow only one space between words. That is, total number of spaces between words or characters should only be one.
It should ignore leading and trailing spaces.
Matches:
Test
Test abc
Non Matches:
Test abc def
Test abc --> I wanted to include multiple spaces between the 2 words. However the editor ignores these extra spaces while posting a question.
Assuming there must be either one or two 'words' (i.e. sequences of non-space characters)
"\s*\S+(\s\S+)?\s*"
Change \S to [A-Za-z] if you want to allow only letters.
Pretty straightforward:
/^ *(\w+ ?)+ *$/
Fiddle: http://refiddle.com/gls
Maybe this one will do?
\s*\S+?\s?\S*\s*
Edit: Its a server-encoded regex, meaning that you might need to remove one of those escaping slashes.
How about:
^\s*(\w+\s)*\w+\s*$
Basically I'm trying to code a regex that will accept two words and nothing else, the words can contain any letters, but not numbers.
I've currently got:
^[a-zA-Z+#-.0-9]/s^[a-zA-Z+#-.0-9]$
Although I know for sure this is wrong because it isn't allowing two words separated by a space, it also currently allows numbers.
Does anybody know what Regex code I need to get this working?
This should do the trick! :)
//Allow letters and numbers
^\w+\s\w+$
//Allow only letters
^[a-zA-Z]+\s+[a-zA-Z]+$
While something using the \w shorthand character class might work for you, you specifically wrote can contain any letters, but not numbers, so you'd have to use:
^[a-zA-Z]+\s+[a-zA-Z]+$
Your expression allows numbers (and + and any of these characters: #$%&*()-',.) because you included all of these characters in your character class [a-zA-Z+#-.0-9], which means lowercase and uppercase letters, + sign, any ASCII characters from # to . (which includes $%&*()-',), and any numbers 0-9.
The shorthand character class \w allows letters, numbers, and underscore (_)
I might recommend running through a short tutorial on regex before deciding its the solution for you...
How about something like:
^\w+\s+\w+$
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.
What is the difference between below two regular expressions
(.|[\r\n]){1,1500}
^.{1,1500}$
The first matches up-to-1500 chars, and the second (assuming you haven't set certain regex options) matches a first single line of up-to-1500 chars, with no newlines.
. does not match new lines.
The second one matches the first 1500 characteres of a line IF the line contains 1500 characters or less
First expression matches some <= 1500 characters of the file(or other source).
Second expression matches a entire line with charsNumber <= 1500.
. matches any character except \n newline.
If it's for use in a RegularExpressionValidator, you probably want to use this regex:
^[\s\S]{1,1500}$
This is because the regex may be run on either the server (.NET) or the client (JavaScript). In .NET regexes you can use the RegexOptions.Singleline flag (or its inline equivalent, (?s)) to make the dot match newlines, but JavaScript has no such mechanism.
[\s\S] matches any whitespace character or anything that's not a whitespace character--in other words, anything. It's the most popular idiom for matching anything including a newline in JavaScript; it's much, much more efficient than alternation-based approaches like (.|\n).
Note that you'll still need to use a RequiredFieldValidator if you don't want the user to leave the textbox empty.
I have a regular expression
^[a-zA-Z+#-.0-9]{1,5}$
which validates that the word contains alpha-numeric characters and few special characters and length should not be more than 5 characters.
How do I make this regular expression to accept a maximum of five words matching the above regular expression.
^[a-zA-Z+#\-.0-9]{1,5}(\s[a-zA-Z+#\-.0-9]{1,5}){0,4}$
Also, you could use for example [ ] instead of \s if you just want to accept space, not tab and newline. And you could write [ ]+ (or \s+) for any number of spaces (or whitespaces), not just one.
Edit: Removed the invalid solution and fixed the bug mentioned by unicornaddict.
I believe this may be what you're looking for. It forces at least one word of your desired pattern, then zero to four of the same, each preceded by one or more white-space characters:
^XX(\s+XX){0,4}$
where XX is your actual one-word regex.
It's separated into two distinct sections so that you're not required to have white-space at the end of the string. If you want to allow for such white-space, simply add \s* at that point. For example, allowing white-space both at start and end would be:
^\s*XX(\s+XX){0,4}\s*$
You regex has a small bug. It matches letters, digits, +, #, period but not hyphen and also all char between # and period. This is because hyphen in a char class when surrounded on both sides acts as a range meta char. To avoid this you'll have to escape the hyphen:
^[a-zA-Z+#\-.0-9]{1,5}$
Or put it at the beg/end of the char class, so that its treated literally:
^[-a-zA-Z+#-.0-9]{1,5}$
^[a-zA-Z+#.0-9-]{1,5}$
Now to match a max of 5 such words you can use:
^(?:[a-zA-Z+#\-.0-9]{1,5}\s+){1,5}$
EDIT: This solution has a severe limitation of matching only those input that end in white space!!! To overcome this limitation you can see the ans by Jakob.