I am using the regular expression of the date for the format "MM/DD/YYYY" like
"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"
its working fine, no problem....here I want to limit the year between "1950" to "2050", how to do this, can anyone help me....
So the answer depends on how you want to accomplish the task.
Your current Regex search pattern is going to match on most dates in the format "MM/DD/YYYY" in the 20th and 21st century. So one approach is to loop through the resulting matches, which are represented as string values at this point, and parse each string into a DateTime. Then you can do some range validation checking.
(Note: I removed the beginning ^ and ending $ from your original to make my example work)
string input = "This is one date 07/04/1776 and this is another 12/07/1941. Today is 08/10/2019.";
string pattern = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d";
List<DateTime> list = new List<DateTime>();
foreach (Match match in Regex.Matches(input, pattern))
{
Console.WriteLine(match.Value);
DateTime result;
if (DateTime.TryParse(match.Value, out result))
{
if (result.Year >= 1950 && result.Year <= 2050)
{
list.Add(result);
}
}
}
Console.WriteLine("Number of valid dates: {0}", list.Count);
This code outputs the following, noting that 1776 is not matched, the other two dates are, but only the last one is added to the list.
12/07/1941
08/10/2019
Number of valid dates: 1
Although this approach has some drawbacks, such as looping over the results a second time to try and do the range validation, there are some advantages as well.
The built-in DateTime methods in the framework are easier to deal with, rather than constantly adjusting the Regex search pattern as your acceptable range can move over time.
By range checking afterward, you could also simplify your Regex search pattern to be more inclusive, perhaps even getting all dates.
A simpler Regex search pattern is easier to maintain, and also makes clear the intent of the code. Regex can be confusing and tricky to decipher the meaning, especially for less experienced coders.
Complex Regex search patterns can introduce subtle bugs. Make sure you have good unit tests wrapped around your code.
Of course your other approach is to adjust the Regex search pattern so that you don't have to parse and check afterwards. In most cases this is going to be the best option. Your search pattern is not returning any values that are outside the range, so you don't have to loop or do any additional checking at that point. Just remember those unit tests!
As #skywalker pointed out in his answer, this pattern should work for you.
string pattern = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19[5-9][0-9]|20[0-4][0-9]|2050)";
year 1950-2050 both inclusive can be found using 19[5-9][0-9]|20[0-4][0-9]|2050
Related
I'm trying to add some validation in ASP.NET using Regex. Essentially I need to ensure a text box includes both ***ThisString*** and ***ThatString*** including the asterisks.
I can get it to work with one, or one or the other, just not both being present at the same time and at any part of the string.l it's validating.
Thanks
As nanhydrin correctly pointed out, my solution will not work if there are multiple of one of the strings but not the other. If that case may occur, you can check for each string separately for readability's sake
First regular expression- (?:\*{3}ThisString\*{3})
Second regular expression- (?:\*{3}ThatString\*{3})
If matches are found in both cases, you're good to go!
Original Answer:-
This is the regular expression you want: (?:\*{3}(?:ThisString|ThatString)\*{3})
Note: Make sure to have global match on and be sure to escape the asterisks correctly.
If the above expression finds 2 (or more) matches, it means you're good to go.
Explanation:-
The entire thing is in a non capturing group, this is to ensure, everything within does get matched fully
There are 3 stars on each side of the strings, having 3 stars on one side but not the other will not result in a match
Both ThisString and ThatString are in a grouped alternative, this is to reduce clutter, you could totally jam every possible positional pattern but this is just better as position doesn't matter here. ***ThatString*** can come before ***ThisString*** or vice versa.
MAKE SURE to check the length of the matches found, the length must be 2 for your
described condition to be satisfied.
Here's the live demo
Using #Chase's answer I was able to come up with the following:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
Regex thisString = new Regex("(?:\*{3}ThisString\*{3})");
Regex thatString = new Regex("(?:\*{3}ThatString\*{3})");
if (!thisString.IsMatch(value.ToString()) || !thatString.IsMatch(value.ToString()))
{
return new ValidationResult("***ThisString*** and ***ThatString*** are used to generate the email text. Please ensure the text above has both ***ThisString*** and ***ThatString*** somewhere within the text."); }
return ValidationResult.Success;
}
Now if either regex patterns don't match anywhere, it'll return an error.
I have a previous question listed here (Searching for specific string pattern) but there are some additional questions that I have.
Previously, I thought my file naming convention was only of these formats:
"aaaaa-ttttt-eeee-q4-2015-file"
"aaaaaa-fffff-3333-q2-2012-file"
or specifically, it is the quarter followed by "-" then year.
However, upon further investigation, the files have other variations such as:
"aaaaaa-f2q09-bbbbb"
"aaaaaa-f2q2008-bbbbb"
"aaaaaa-f4q-2008-fffff"
"f4q-aaaaa-eeeeee-2008"
"q2-aaaaaaaaa-eeeeeee-2005"
"aaaaaaaa-3q-2008-rrrrrrr"
Similarly for all the above, I would like to extract the year and quarter, and I'm not sure if there is a general code that I can write that can extract them all at one go or do i have to write a few sets of code and run them by waves. Not very familiar with sub function in R and would actually appreciate if someone can point me to a website that has detailed explanations and examples for me to write my own code to extract these info.
Ultimately, the code should parse all those strings and output something like: year = 2005, quarter = q4 etc.
Try this it uses regexpr to show the location of the match and regmatches to return them, it is very susceptible to pull out incorrect data. For quarter it will return any instance of 1-4 either followed or preceded by a q. If there is any other information that can make these more specific matches than I suggest including them.
input=c("aaaaaa-f2q09-bbbbb",
"aaaaaa-f2q2008-bbbbb",
"aaaaaa-f4q-2008-fffff",
"f4q-aaaaa-eeeeee-2008",
"q2-aaaaaaaaa-eeeeeee-2005",
"aaaaaaaa-3q-2008-rrrrrrr")
quarter=regmatches(input, regexpr("[1-4]q|q[1-4]", input))
year = regmatches(input, regexpr("q\\d{4}|q\\d{2}|\\d{4}", input))
year = gsub("q","",year)
year = sub("\\b(\\d{2})\\b","20\\1", year)
There are lots of issues with the year matching also, because you have three different formats that are possible "q09", "q2008", "2008". Because the function returns the first match in the string the q\d{4} is needed to pull back the q2008 example.
My sub function here subs that matching regular expression with 20 and the matching expression itself, the \\1 is returning the stuff in brackets (\\d{2})
Test it and comment any mistakes
I have been using strapplyc in R to select different portions of a string that match one particular set of criteria. These have worked successfully until I found a portion of the string where the required portion could be defined one of two ways.
Here is an example of the string which is liberally sprinkled with \t:
\t\t\tsome words here\t\t\tDefect: some more words here Action: more words
I can write the strapply statement to capture the text between Defect: and the start of Action:
strapplyc(record[i], "Defect:(.*?)Action")
This works and selects the chosen text between Defect: and Action. In some cases there is no action section to the string and I've used the following code to capture these cases.
strapplyc(record[i], "Defect:(.*?)$")
What I have been trying to do is capture the text that either ends with Action, or with the end of the string (using $).
This is the bit that keeps failing. It returns nothing for either option. Here is my failing code:
strapplyc(record[i], "Defect:(.*?)Action|$")
Any idea where I'm going wrong, or a better solution would be much appreciated.
If you are up for a more efficient solution, you could drop the .*? matching and unroll your pattern like:
Defect:((?:[^A]+|A(?!ction))*)
This matches Defect: followed by any amount of characters that are not an A or are an A and not followed by ction. This avoids the expanding that is needed for the lazy dot matching. It will work for both ways, as it does stop matching when it hits Action or the end of your string.
As suggested by Wiktor, you can also use
Defect:([^A]*(?:A(?!ction)[^A]*)*)
Which is a little bit faster when there are many As in the string.
You might want to consider to use A(?!ction:) or A(?!ction\s*:), to avoid false early matches.
The alternation operator | is the regex operator with the lowest precedence. That means the regex Defect:(.*?)Action|$ is actually a combination of Defect:(.*?)Action and $ - since an empty string is a valid match for $, your regex returns the empty string.
To solve that, you should combine the regexes Defect:(.*?)Action and Defect:(.*?)$ with an OR:
Defect:(.*?)Action|Defect:(.*?)$
Or you can enclose Action|$ in a group as Sebastian Proske said in the comments:
Defect:(.*?)(?:Action|$)
I am not expert in writing regular expressions so need your help. I want to validate date in "dd-MMM-yyyy" format i.e. 07-Jun-2012. I am using RegularExpressionValidator in asp.net.
Can anybody help me out providing the expression?
Thanks for sharing your time.
Using a DatePicker is probably the best approach. However, since that's not what you asked, here's an option (although it's case sensitive):
^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$
In addition, here's a place you can easily test Regular Expressions: http://www.regular-expressions.info/javascriptexample.html
Regex without leading zero in day.
^\d{1,2}-[a-zA-Z]{3}-\d{4}$
Update Regex with leading zero in day.
^\d{2}-[a-zA-Z]{3}-\d{4}$
It's not regex, but you can use build in DateTime.TryParseExact function to validate your datetime string
DateTime dateTime;
string toValidate = "01-Feb-2000";
bool isStringValid = DateTime.TryParseExact(
toValidate,
"dd-MMM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateTime);
The accepted solution allows '00' as the day, so here is a fix for that:
^(([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$
Notes/Exceptions:
1.Be aware of case sensitivity issues. Eg. 'DEC' will not pass while 'Dec' will pass. You may want to convert the regex string and test string to lowercase before testing (if your application allows).
2.This will not catch days that don't exist, like Feb 30th, June 31st, etc.
"\d{4}\d{2}\d{2}|\d{2}/\d{2}/\d{4}|\d{2}.\d{2}.\d{4}|\d{2}\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2}\-\d{4}|\d{4}-\d{2}-\d{2}"
These format is mm.dd.yyyy, d-MMM, mm.dd.yyyy
Yet another idea would be to try this (similar to user1441894's idea):
var x = DateTime.Parse("30-Feb").GetDateTimeFormats();
I learned to use this yesterday (for a different purpose). So try-catch this statement to deal with validity/invalidity of the date :)
"^(([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1])[-]([JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC])[-](d{4}))$"
using System.Text.RegularExpressions
private void fnValidateDateFormat(string strStartDate,string strEndDate)
{
Regex regexDt = new Regex("(^(((([1-9])|([0][1-9])|([1-2][0-9])|(30))\\-([A,a][P,p][R,r]|[J,j][U,u][N,n]|[S,s][E,e][P,p]|[N,n][O,o][V,v]))|((([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))\\-([J,j][A,a][N,n]|[M,m][A,a][R,r]|[M,m][A,a][Y,y]|[J,j][U,u][L,l]|[A,a][U,u][G,g]|[O,o][C,c][T,t]|[D,d][E,e][C,c])))\\-[0-9]{4}$)|(^(([1-9])|([0][1-9])|([1][0-9])|([2][0-8]))\\-([F,f][E,e][B,b])\\-[0-9]{2}(([02468][1235679])|([13579][01345789]))$)|(^(([1-9])|([0][1-9])|([1][0-9])|([2][0-9]))\\-([F,f][E,e][B,b])\\-[0-9]{2}(([02468][048])|([13579][26]))$)");
Match mtStartDt = Regex.Match(strStartDate,regexDt.ToString());
Match mtEndDt = Regex.Match(strEndDate,regexDt.ToString());
if (mtStartDt.Success && mtEndDt.Success)
{
//piece of code
}
}
I am looking for a Regular expression to match only if a date is in the first 28 days of the month. This is for my validator control in ASP.NET
Don't do this with Regex. Dates are formatted differently in different countries. Use the DateTime.TryParse routine instead:
DateTime parsedDate;
if ( DateTime.TryParse( dateString, out parsedDate) && parsedDate.Day <= 28 )
{
// logic goes here.
}
Regex is nearly the golden hammer of input validation, but in this instance, it's the wrong choice.
I don't think this is a task very well-suited for a regexp.
I'd try and use the library functions (DateTime.Parse for .NET) to parse the date and then check the day component of it. Everything else is duplicating half the library function anyways.
Why not just covert it to a date data type and check the day? Using a regular expression, while it could be done, just makes it overly complicated.
([1-9]|1\d|2[0-8]) // matches 1 to 28 but woudn't allow leading zeros for single digits
(0?[1-9]|1\d|2[0-8]) // matches 1 to 28 and would allow 01, 02,... 09
(where \d matches any digit, use [0-9] if your regex engine doesn't support it.)
See also the question What is the regex pattern for datetime (2008-09-01 12:35:45 ) ?
I would use one of the DateTime.TryParse techniques in conjunction with a CustomValidator