Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have datetime string liek this 5/33/2012. I want to get only 33 here. How can I do that using regular expression in C# ?
Posted

The value between slashes can be retrieved using the Regex class as shown below:
C#
string date = @"5/33/2012";
string valueInSlashes = Regex.Match(date,@"(?<=/\s*)\d*(?=\s*/)",
    RegexOptions.CultureInvariant).Value;
//valueInSlashes = 33

The pattern @"(?<=/\s*)\d*(?=\s*/)" matches digits within slashes.
 
Share this answer
 
Comments
Rahul Rajat Singh 4-Jun-12 5:07am    
Perfect. +5.
VJ Reddy 4-Jun-12 5:15am    
Thank you, Rahul :)
Manas Bhardwaj 4-Jun-12 15:36pm    
on the spot +5
VJ Reddy 4-Jun-12 19:42pm    
Thank you, Manas :)
touseef4pk 5-Jun-12 6:47am    
thanks
Hi,

Are you looking for below code ?

C#
string str = "3/33/3333";
           Regex regex = new Regex(@"(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>(?:\d{4}|\d{2}))",
                           RegexOptions.IgnoreCase
                           | RegexOptions.Multiline
                           | RegexOptions.IgnorePatternWhitespace
                           | RegexOptions.Compiled
                           );

           Match m = regex.Match(str);
           var abc = m.Groups["Day"].Value;


in above code abc will gives you desired result.

above code is take from this

Thanks
-Amit
 
Share this answer
 
v2
Comments
taha bahraminezhad Jooneghani 4-Jun-12 4:50am    
when you can solve the problem in 2line code?
he ask for regex probably he don't know that there is a easy way with out regex!
if he just want regex your solution is pretty good!
AmitGajjar 4-Jun-12 4:52am    
even single line can do that :)

str.Split('/')[1]
taha bahraminezhad Jooneghani 4-Jun-12 5:35am    
absolutely right!:)
repalce string with "/"
C#
function string Repalce()
{
    string str="5/33/2012";
    str.replace("/","")
    return str;//You get here "5332012"
}

you want 33
then
C#
"5332012"
str.substring(1,2)
return str;//You get here 33
 
Share this answer
 
v5
Comments
jim lahey 4-Jun-12 3:42am    
where's the regular expression?
kodeLogic 4-Jun-12 3:46am    
Needs to check if the month is a one digit (str.substring(1,2)) or two digits (str.substring(2,2)).
AmitGajjar 4-Jun-12 4:14am    
even if you do not want to use Regex, this is not right way... totally disagree....

you can use str.Split('/')[1] instead.
[no name] 4-Jun-12 4:16am    
ya i have not used regular expression but using this way we can also do.
Don't know the exact regex, but I'm sure you'll find what you need here:

http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5&AspxAutoDetectCookieSupport=1[^]

On a side note, the string you've posted isn't a valid datetime.
 
Share this answer
 
C#
string dates = "22/33/2012";
string[] date = dates.Split('/');
string newdate =  date[1];
 
Share this answer
 
Comments
AmitGajjar 4-Jun-12 4:18am    
use Regular expression. see my answer.
the solution is like

C#
string sDt = "5/33/2012";
        string[] sVar = sDt.Split("/");
        string sYourValue = sVar[1];
 
Share this answer
 
Comments
AmitGajjar 4-Jun-12 4:17am    
there is no regular exp!!!! check my answer.
C#
string dates = "5/30/2012";
string day = DateTime.Parse(dates).Day;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900