Click here to Skip to main content
12,953,700 members (51,279 online)
Rate this:
 
Please Sign up or sign in to vote.
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 3-Jun-12 21: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 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
VJ Reddy 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 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 bahrami 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 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.
NikhildPawar 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 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 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
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
ppolymorphe 2,020
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2017
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