Regex for Date in format dd.MM.yyyy? - c#

Trying to create a regex for Date in this format dd.MM.yyyy.
I want to use it with a DataAnnotation, like this
[RegularExpression(#"<theregex>")]
public DateTime Date { get; set; }

I suggest that regex is not exactly the best way to do this. You haven't given the context, so it's hard to guess what you're doing overall... but you might want to create a DateExpression attribute or such and then do:
return DateTime.ParseExact(value, "dd.MM.yyyy");
in wherever your converter is defined.

^(0[1-9]|[12][0-9]|3[01])[.](0[1-9]|1[012])[.](19|20)[0-9]{2}$
This regex matches 01.01.1900, 01.01.2000 but doesn't match 1.1.2000 or 1/1/00.

[0-3]{0,1}[0-9]\.[0-1]{0,1}[0-9]\.[0-9]{4,2}
matches:
28.2.96, 1.11.2008 and 12.10.2005

Just because I always find this site useful, here is an online regex checker. On the right hand side it also has examples and community contributions. In there are a number of Date matching regex variations.
You can type in a load of dates you wish to match and try some of the different examples if you're not sure which is best for you. As with anything, there are multiple ways to solve the problem, but it might help you choose the one that fits best.

^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
This one also accepts - and / as separators. You can remove them if you want.

Related

i can define a String.contain("-") to search for specific character "`-`" followed by one or more integers

i have a question if i can define a String.contain("-") to search for specific character "-" followed by one or more integers.
so it will cover something such as :-
search -12
t-123est
but will not cover
search-t12
t-est123
You best option might not be to use String.Contains, You might be best served using Regex.IsMatch. With that you can define a regular expression that will exactly match your needs. you can use sites like https://www.regex101.com/ to test your expression to make sure it covers your cases. In your case, you can use
Regex.IsMatch(myString, #"-\d+")
This would be enough:
Regex.IsMatch("search -12", #"-\d")

RegEx to match M/YYYY, MM/YYYY , M/YY or MM/YY format but not MM/DD/YYYY

Okay, I have fought with this long enough. I have a regex
^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)
which will match 8/15, 08/15, 8/2015, 08/2015 all of which I need. So far so good. But what I don't need is for it to match 08/01/2015 but I get a partial match on 08/01. How do I ignore 08/01/2015?
DateTime.ParseExact is a much more readable and maintainable way to address your issue: https://msdn.microsoft.com/en-us/library/332de853(v=vs.110).aspx
If I were your colleague, I would hate reading this ugly regex when the same concept can be so elegantly expressed with a designated library class.
Actually an even better idea is TryParseExact: https://msdn.microsoft.com/en-us/library/h9b85w22(v=vs.110).aspx
As you can see, there's an overload that accepts multiple formats, which is exactly your case and you can pass ["M/yyyy", "MM/yyyy, "M/yy", "MM/yy"].
Here's a code example for a case very similar to yours: https://stackoverflow.com/a/18247797/326370
You can use the following regex if your string is starting with digit.
^\d{1,2}\/(?:\d{2}|\d{4})(?!\/)

I keep getting .success as false

I have to read an excel file and check if its name follows a certain pattern.
The pattern is CDFSDDRCxxxCurryymmdd, where xxx is a number, Curr is either EUR or GBP and yymmdd is a date.
This is my file's name, CDFSDDRC603EUR120124.xls. Can also be .xlsx.
And this is my code.
Match nameIsValid = Regex.Match(activeWorkbook.Name,
#"CDFSDDRC(?<xxx>[0-9]+)(?<xxx>[A-Z]+)(?<yymmdd>[0-9][0-9][0|1][0-9][0-3][0-9])\.xls?");
I keep getting nameIsValid.success as false.
I think my Regex expression is ok, but then again.
Any ideas?
Rui Martins
Your pattern for the date matching is somewhat messed up. It doesn't really match dates but just a weir combination of numbers. Also, you can't use | inside character classes. Think closely about how dates are constructed.
Furthermore, you have a duplicate matching group called xxx. This will surely mess up C#'s behaviour when accessing one of them.
Try this one
/CDFSDDRC(?<xxx>\d+)(?<curr>[A-Z]+)(?<yymmdd>\d{2}(?:0[1-9]|1[12])(?:(?:0|1|2)[1-9]|3[0-2]))\.xlsx?/
CDFSDDRC(?<xxx>\d+)(?<curr>EUR|GBP)(?<yymmdd>\d{2}[0-1]\d[0-3]\d)\.xlsx?
The following works for me for both xls and xslx.

A regular expression to validate .NET time format

Background
I need to validate user input in some fields, where these are defining how to show time in some views.
Requirements
Time format must be expressed in Microsoft .NET way (check this MSDN Library article if you want to learn more about framework's date and time formatting: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx)
Keep in mind I'm looking to validate the format instead of an actual time string.
For example, user may input:
HH:mm
hh:mm
ss
hh:ss
mm:ss
... and so on.
In fact, it should validate from the shortest to longest time format available.
Another point is I need to do it in client-side using JavaScript. In other words, any given regular expression by you should work in browsers JavaScript regular expressions' engine.
I'll appreciate any self-taylored one, any link or pasted expression!
Thank you in advance.
NOTE (Update)
I can't use ASP.NET validation engine, or any other. Because of project's requirements, I need to avoid that.
As far as I understand, there is no much options - sort of 20, as maximum. Why not just enumerate them all in one big regex without much special symbols? Like
'hh:mm|hh:mm:ss|yyyy-MM-dd hh:mm|<etc>'
you could than make it case sensitive to differentiate between M for month and m for minute, and for hours make it [hH], then make it [:-/] there where you allow for different separators, and lots of other similar things. But the main idea is to simply enumerate all options separated by | with just little amount of regex syntax between | and |.
What is your definition of a "valid" format string? Only once you know that can it be possible to validate a format string.
"K" is also a valid format string
"zz" is also a valid format string
"e" is also a valid format (it would fall into the "The character is copied to the result string unchanged." case)
I'm not even sure what formats would actually cause .NET .ToString() to throw an exception (if that's what you are trying to avoid).

What's a C# regular expression that'll validate currency, float or integer?

What is a regular expression suitable for C# that'll validate a number if it matches the following?
$1,000,000.150
$10000000.199
$10000
1,000,000.150
100000.123
10000
Or the negative equivalents?
You can use csmba's regex if you make one slight modification to it.
^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{2})?)$
I think ssg is right.
It's not a really good use of Regex, especially if your software has to deal with non-US centric data entry.
For instance, if the currency symbol is the Euro, or the Japanese Yen or the British Pound any of the other dozen currency symbols out there?
What about number formatting rules?
In the US you would enter 1,000,000.00 but in France, this should be 1.000.000,00. Other countries allow spacing between digit-grouping...
If you use a straight Regex without taking the Culture into account, then you're never going to validate successfully unless you're 100% sure your software will never ever be used in a non-US centric context.
^\$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{2})?)$
I think I've found a problem with ssg's solution (or perhaps an MS bug!).
Running this:
float.TryParse("0,2",NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"), out num)
Returns true. Surely "0,2" isn't a valid currency value?
Be careful with floats. Eventually you will hit a case such as 0.01 represented as 0.00999999. Strings or integers are better to use.
Try this one. It may need some fine tuning to only allow for a single decimal point, but it does match your test cases. I hope this helps.
[$\d,.]+
Use this regular expression for US currency
\$(\d)*\d
Matches $300,$12900
Non-Match $12900.00
This regular Expression works for me:
'^[$]{0,1}([0-9]+[,]?[0-9]+|[0-9]{1,3}([.][0-9]{3})*([,][0-9]+)?)$'
with switch
'^\${0,1}(\d+,?[0-9]+|\d{1,3}(\.\d{3})*(,\d+)?)$'
it works for
$1,000,000.150
10000000.199
$10000
1,000,000.150
100000.123
10000

Categories

Resources