This question already has answers here:
Regular expression for not allowing spaces in the input field
(5 answers)
Closed 3 years ago.
I'm trying to use regex which checks only two things
Minimum 10 characters (No Max)
No whitespace allowed
I'm able to check minimum 10 chars with #"^[a-zA-Z0-9]{10,}$" and disallow white space with ^[^0-9 ]+$
Now the problem is, how to combine both of these and allow everything(alphanumeric including special characters) except white space
You could try to use a simpler regex pattern just to accept anything that is not a white-space: ^\S{10,}$
\S - matches any non-white-space character. More details here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
Related
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 months ago.
I am absolutely clueless when it comes to Regex strings. I am trying to create a custom validator on a model using [RegularExpression("myValidator")] How can I create a regex expression to validate the following formats
######-##
######-#
where # is a number. Could someone help me out?
Thanks!
\d means digit.
{N} means previous symbol repeated N times
so, basically you want:
\d{6}-\d{2}
which would match 6 digits, a dash, and 2 more digits.
You can also do:
\d{6}-\d{1,2}
which would match 6 digits, a dash, and then 1 or 2 more digits, and therefore work for either format you described.
This question already has answers here:
How do I remove all non alphanumeric characters from a string except dash?
(13 answers)
Closed 3 years ago.
I have some text, I need to select only alphanumeric characters only in single match.
I have tried this in regex
[^\W]+
Pattern : [^\W]+
Input: This is "My Page"
https://rubular.com/r/PMQwahJIqqiOOI
Output I Need: This is My Page
Remove everything that is not word character or space using this regex,
[^\w ]+
with empty space.
Regex Demo
This question already has answers here:
Regex to match at least 2 digits, 2 letters in any order in a string
(4 answers)
Closed 4 years ago.
I want to allow only digits, spaces and these characters:
\
+
(
)
I'd like it to be a minimum of 6 digits. The digits can be anywhere in the string.
This is the closest I can get it:
^(?=.{6,})[0-9\-\+\(\\) ]*$
My code works except for the minimum of 6 digits requirement. (It just enforces a minimum of 6 characters.)
Input text that should not match:
+()1234
Input text that should match:
+(44) 78666-05529
Your current lookahead ^(?=.{6,}) asserts that what follows is 6 or more times times any character from the start of the string.
If the digits can be anywhere in the string you have to assert a digit 6 times using a positive lookahead and a non capturing group (?:.*[0-9]){6}.
Note that this does not take care of the exact formats of the numbers in the example data.
^(?=(?:.*[0-9]){6})[0-9\-+(\\) ]*$
Regex demo
This question already has answers here:
How can you strip non-ASCII characters from a string? (in C#)
(15 answers)
C# regex to remove non - printable characters, and control characters, in a text that has a mix of many different languages, unicode letters
(4 answers)
Closed 4 years ago.
I'm reading data from a file, and sometimes the file contains funky stuff, like:
"䉌Āᜊ»ç‰ç•‡ï¼ƒè¸²æœ€ä²’Bíœë¨¿ä„€å•²ï²ä‹¾é¥˜BéŒé“‡ä„€â²ä‹¾â¢"
I need to strip/replace these characters as JSON has no idea what to do with them.
They aren't control characters (I think), so my current regex of
Regex.Replace(value, #"\p{C}+", string.Empty);
Isn't catching them.
A lot of these strings read in are going to be long, upwards of256 characters, so I'd rather not loop through each char checking it.
Is there a simple solution to this? I'm thinking regular expressions would solve it, but I'm not sure.
If all you want is ASCII then you could do:
Regex.Replace(value, #"[^\x00-\x7F]+", string.Empty);
and if all you want are the "normal" ASCII characters, you could do:
Regex.Replace(value, #"[^\x20-\x7E]+", string.Empty);
This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
Closed 6 years ago.
I need to detect following format when I enter serial number like
CK123456.789
I used Regex with pattern of
^(CV[0-9]{6}\.[0-9]{3}
to match but if I enter
CK123456.7890
it still able to proceed without flagging error. Is there a better regular expression to detect the trailing 3 digits after '.'?
Depending on how you use the regular expression matcher, you might need to enclose it in ^...$ which forces the pattern to be the whole string, i.e.
^CK[0-9]{6}\.[0-9]{3}$ (Note the CK prefix).
I've also removed your leading (mismatched) parenthesis.