Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 3-Jun-12 22:35pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

The value between slashes can be retrieved using the Regex class as shown below:
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.
  Permalink  
Comments
Rahul Rajat Singh at 4-Jun-12 5:07am
   
Perfect. +5.
VJ Reddy at 4-Jun-12 5:15am
   
Thank you, Rahul :)
Manas Bhardwaj at 4-Jun-12 15:36pm
   
on the spot +5
VJ Reddy at 4-Jun-12 19:42pm
   
Thank you, Manas :)
touseef4pk at 5-Jun-12 6:47am
   
thanks
VJ Reddy at 5-Jun-12 6:56am
   
You're welcome and thank you for the response :)
If the solution is helpful then you may accept the solution.
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi,
 
Are you looking for below code ?
 
 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
  Permalink  
v2
Comments
taha bahrami at 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 at 4-Jun-12 4:52am
   
even single line can do that :)
 
str.Split('/')[1]
taha bahrami at 4-Jun-12 5:35am
   
absolutely right!:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

repalce string with "/"
function string Repalce()
{
    string str="5/33/2012";
    str.replace("/","")
    return str;//You get here "5332012"
}
you want 33
then
"5332012"
str.substring(1,2)
return str;//You get here 33
  Permalink  
v5
Comments
jim lahey at 4-Jun-12 3:42am
   
where's the regular expression?
kodeLogic at 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 at 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.
NikhildPawar at 4-Jun-12 4:16am
   
ya i have not used regular expression but using this way we can also do.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

string dates = "22/33/2012";
string[] date = dates.Split('/');
string newdate =  date[1];
  Permalink  
Comments
amitgajjar at 4-Jun-12 4:18am
   
use Regular expression. see my answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

the solution is like
 
 
string sDt = "5/33/2012";
        string[] sVar = sDt.Split("/");
        string sYourValue = sVar[1];
 

  Permalink  
Comments
amitgajjar at 4-Jun-12 4:17am
   
there is no regular exp!!!! check my answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

string dates = "5/30/2012";
string day = DateTime.Parse(dates).Day;
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100