Help me modify this regex - asp.net

im trying to make a regex, and so far it does the folowwing:
-Disallow whitespace at the front and end
-Allow spaces in the middle
Here is the regex:
^[^\s].+[^\s]$
I want it to also disallow special characters, how do i do that?
Thanks

^[a-zA-Z0-9]([a-zA-Z0-9 ]*[a-zA-Z0-9])?$

^[a-zA-Z0-9][a-zA-Z0-9 ]*[a-zA-Z0-9]$ should only allow lowercase, uppercase and numbers and spaces except spaces at the start of end of the string

Related

Regex in asp.net for allowing only characters with single space?

I want to allow a user to enter only characters and with single space in a textbox. What i have tried is :
^[\w\.:\(\)\[\]{}\-_](?: ?[\w\.:\(\)\[\]{}\-_])*$
it is blocking all but allowing digits also . How to make it correct such that it only allow characters and no digits but a single space between words? Thank you.
I'd use:
^[\p{L}.:()[\]{}_-]+(?: [\p{L}.:()[\]{}_-]+)*$
Where \p{L} stands for any letter.
Edit:
I've changed \pL to \p{L} because it's not supported by .NET.
Thanks to Alan Moore.
Is ([A-Za-z])+( [A-Za-z]+) what you're after? Or do you want the regex to only match once?
Assuming there must be either oneor two 'words' (i.e. sequences of non-space characters)
"\s*[A-Za-z] +(\s[A-Za-z] +)?\s*"

Regular expression to allow only one space between words

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*$

Regular Expression to remove contents in string

I have a string as below:
4s: and in this <em>new</em>, 5s: <em>year</em> everybody try to make our planet clean and polution free.
Replace string:
4s: and in this <em>new</em>, <em>year</em> everybody try to make our planet clean and polution free.
what i want is ,if string have two <em> tags , and if gap between these two <em> tags is of just one word and also , format of that word will be of ns: (n is any numeric value 0 to 4 char. long). then i want to remove ns: from that string. while keeping punctuation marks('?', '.' , ',',) between two <em> as it is.
also i like to add note that. input string may or may not have punctuation marks between these two <em> tags.
My regular expression as below
Regex.Replace(txtHighlight, #"</em>.(\s*)(\d*)s:(\s*).<em", "</em> <em");
Hope it is clear to my requirement.
How can I do this using regular expressions?
Not really sure what you need, but how about:
Regex.Replace(txtHighlight, #"</em>(.)\s*\d+s:\s*(.)<em", "</em>$1$2<em");
If you just want to take out the 4s 5s bit you could do something like this:
Regex.Replace(txtHighlight, #"\s\d\:", "");
This will match a space followed by a digit followed by a colon.
If that's not what you're after, my apologies. I hope it might help :)

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.

regex to disallow whitespace

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

Resources