Regex to allow number with comma and proper validation [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Having a hard time getting my regex to work correctly. Essentially, all I need is a valid number regex that just allows for one comma. Here's what I have tried:
[0-9]*[,]\\d
(This was when I thought I might have a number with multiple commas, not the case anymore)
[0-9][,]\\d
and
^\d+(?:[\,]\d+)?$ (http://regexr.com/3ggn5)
The latter seemed to work the best, however when I input this: 1,23134 it doesn't break the rule. How can I make it better to make sure if you input an invalid number 1,23232 (for example) it will break, but be fine if you do 1,232 (for example, just showing a valid number input).
UPDATE
This is the code surrounding, just using a RegularExpression annotation:
[RegularExpression(#"^\d+(?:[\,]\d+)?$", ErrorMessage = ...]
UPDATE 2
By valid number I simply mean a number that is correctly formatted to United States standards. Example of valid numbers:
1
10
100
1,000
1000
10,000
10000
100,000
100000
..etc
In the United States, we either have a comma or don't after the third digit sequentially (except for the first number in some cases, 1,000 is valid). Although, if you have comma, you typically will use commas every third digit. So I would assume a number like this: 1,00000000 isn't valid.
Examples of invalid numbers:
1,1
1,00
12,12
Basically if anywhere else in the world uses a comma in a place that isn't after the third digit, this would be invalid for what I need. Simply just numbers that may or may not have a comma.

This Regex will parse a number in many valid format:
^-?(\d+|\d{1,3}(?:,\d{3})+)?(\.\d+)?$
It will detect too many numbers after comma
wrong dot notation
numbers with no comma will pass
If you don't need nor negative nor float numbers, you can simplify it:
^(?:\d+|\d{1,3}(?:,\d{3})+)$
And if you don't want number without comma either (e.g: 1345):
^\d{1,3}(?:,\d{3})+$
P.S: For users coming from a non-english speaking world, you can replace the comma with a space in all those regex, and it will work the same way

Related

C# Trim() is not working in one of my projects [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have silly problem with Asp.Net project that I'm working on it for more than 5 years.
Today suddenly the Trim() function stopped working.
Notes:
I update project framework from 4.7.2 to 4.8 and the problem still happen.
I tried TrimLeft() and TrimRight() also have the same problem of Trim()
Replace Function is still working fine not effected but it is not a good solution for me to use it every where.
Trim working fine on new projects
I also check the char code of space it is 32
any idea?
this is the code
string val = "abo ‎"; // "abo\x0020\x0020\x0020\x0020\x200e"
string userName = val.Trim();
you can run the problem in this link
Update:
thanks you all for the comments, I also found a simple check to test the end of the char when you set the cursor at the end and press backspace one time nothing is happen the second press start deleting and that because of the \x200e char at the end.
any idea how to trim hidden Char from left and right and deal with just like spaces.
String.Trim works. If it didn't, hundreds of thousands of developers would have noticed 16 years ago.
The string ends with a formatting character, specifically \x200e, the Left-to-Right-Mark. That's definitely not a whitespace. Calling Char.GetUnicodeCategory('‎') returns Format. I suspect the input came from mixed Arabic and Latin text, perhaps something copied by a user from a longer string?
One way to handle this is to use String.Trim(char[]) specifying the LTR mark along with other characters. That's not quite the same as String.Trim() though, which removes any character that returns true with Char.IsWhiteSpace() :
var userName=val.Trim(' ','\t','\r','\n','\u200e`);
Another option would be to use a regular expression that trims both whitespace \s and characters in the Format Unicode category Cf, only from the start ^[\p{Cf}\s] or end ([\p{Cf}\s]+$) :
string userName = Regex.Replace(val,#"(^[\p{Cf}\s]+)|([\p{Cf}\s]+$)","");
Perhaps a better option would be to prevent unexpected characters using input validation, and require that the input TextBox contains only letter or letter and digit characters. After all, the user could paste some other unexpected non-printable character. It's better to warn the user than try to handle all possible bad data.
Usernames are typically letter and number combinations without whitespace. All ASP.NET stacks allow validation. Modern browsers allow regular expression validation in the input element directly, so we could come up with a regex that allows only valid characters, eg :
<input type="text" required pattern="[A-Z0-9]' ..../>
The NumberLetter block (Nl) could be used to capture numbers and letters in any language, just like Cf is used to capture format characters

ASP.NET Password Validation contain characters from 2 sets [duplicate]

This question already has answers here:
Regular expression to check if password is "8 characters including 1 uppercase letter, 1 special character, alphanumeric characters"
(14 answers)
Closed 5 years ago.
I am re-writting our password validation rules to meet a very strict set of requirements for a asp.net c# web forms application.
I am using regex from most of this (such as min characters, allowed characters etc).
One requirement that I am having trouble with finding the solution for is the following:
Must contain characters from 2 out of the 4 allowed character classes.
In this case the allowed classes are uppercase, lowercase, numeric and special (US ASCII).
If anyone can help on how to write this that would be apprecicated.
Thanks
The commenters all express valid concerns, but I'd just like to share this method for matching "at least 2 of 4 elements":
(?:.*?(?:a(?!.*a)|b(?!.*b)|c(?!.*c)|d(?!.*d))){2}
The letters 'a' to 'd' can be replaced with appropriate character classes for this question, but it can also be extended to more generally match "at least X of Y non-overlapping subexpressions".

English Language Regex in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need a regex pattern which can detect if the given text is in English or not, but I want to include the following:
Allowing spaces
Allowing numbers and words
Allowing multiple lines and tabs
Allowing all special characters !##$%^&*()_-+={}|/<>~`':";[]
Allowing URLs, emails
If the given text contains any character rather than English, it should be considered a non-English text, this should be applied if the text contains Arabic letters/words like "ا ب ت ... etc." and the same for French "é, â ... etc." and also all of the other languages
In brief, I need to know if the given text, any text with any format, is in English or not. I tried a lot of patterns but I didn't get it, and actually, I don't need to use any language detector as the application will be used offline.
Samples of the texts which should not be accepted:
Hello! ... é
مرحبا بك
للتحميل اضغط هنا ... http://www.google.com
So, if the text contains non-English letter, it should be considered non-English text.
I think I found it, I tried the Basic Latin Unicode category, and it works fine so far. I used:
"^[\u0000-\u007F]+$"
Its idea is about checking if the given text is in English and is written by using English letters only, in addition, it allows special characters. So, if the given text was like this "I met my friend in a café", it is considered as non-English text, as the given text should contain only English letters and avoid any other letters even if typed a name, place ... etc. this was exactly what I need.
Thank you all.
Resources:
http://kourge.net/projects/regexp-unicode-block
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
Regular expression to match non-English characters?
In theory it is possible, if regex contained every word from English dictionary.
You can create a regex that detects non-English characters. That will detect text that is definitely not English, but won't be able to confirm it definitely is.
This should work:
#"[^\t\w\d\s$-/:-?{-~!"^_`\[\]]+"
If there is a match, there ARE non-english letters/characters.
BTW, you are just testing if the text contains only those characters where a English speaking person would normally use, NOT what language it is in.
To detect a language you need stuffs like Natural Language Processing but NOT regex.

Regular Expression - One Item Has Multiple Words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have multiple lines in a text file.
The text file looks similar to this:
Column 1 Column 2 Column 3
12345 stack overflow 12345678
I need a regular expression to check this and then grab column two. My problem is column two can be one or multiple words and I need it to be one item in the string when I grab it or grab other columns.
Read the file line by line and match it using the following regex:
^\d*\s*([\w\s]*\w)\s*\d*$
Now, the 1st named subgroup should give you what you need. I'm not exactly sure about C# syntax, but for notepad++, $1 works well.
The ^ ensures that the regex starts matching from the very beginning of the read line and the $ ensures that it matches up till the very end.
The default greedy matching of the regex assures that no extra spaces are captured in the beginning of the column two content and the \w at the end ensures no trailing spaces.
If the carriage return and new-line characters are read by your platform as well, you can modify it as:
^\s*\d*\s*([\w\s]*\w)\s*\d*\s*$

Regex Directory Name Validation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to check if a textbox have a valid directory name. Because I will create a directory with this textbox value.
Another thing, the value must have at least 3 characters and cannot have more than 20 characters.
How can I do it?
Path.GetInvalidPathChars is where you can find out which characters are invalid. Rather than use regex, I suggest you use Path.GetFullPath as this will validate the path for you: invariably it will do a better job than anything you will try to roll yourself and will be kept up-to-date as the rules change over time.
As for its length, use the methods of the Path class to get the component of the path that you want to check.
No need for RegEx, it's a waste.
public bool ValidName(string dirName)
{
char[] reserved = Path.GetInvalidFileNameChars();
if (dirName.Length < 3)
return false;
if (dirName > 20)
return false;
foreach (char c in reserved)
{
if (dirName.Contains(c))
return false;
}
return true;
}
RegEx isn't particularly efficient and isn't really necessary here. Just check the bounds then make sure the string doesn't contain any reserved characters, returning false as soon as you find an error.
Easy
this is regex you should use.
^[0-9A-Za-Z_-]{3,20}$
"^"means starts with the characters defined in [] brackets
"[]" represents list of allowed characters
"0-9" represents that numbers from 0-9 can be used
"A-Z" uppercase letters from A to Z
"a-z" lowercase letters from a to z
"_" underscore
"-" dash
"{}" represents limitations
"{3,20}" - min 3 characters max 20
"$" ends with the characters defined in []
if you dont use ^$ than it will search combination of those letters in string so string could be 30 characters and it will be valid.
I hope this helps

Categories

Resources