如何在两个斜杠之间获取字符串 [英] How to get string between two slashes

查看:332
本文介绍了如何在两个斜杠之间获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2012年5月33日有日期时间字符串.我只想在这里得到33.如何使用C#中的正则表达式来做到这一点?

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# ?

推荐答案

可以使用Regex 类检索斜线之间的值,如下所示:
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


模式@"(?<=/\s*)\d*(?=\s*/)"匹配斜杠中的数字.


The pattern @"(?<=/\s*)\d*(?=\s*/)" matches digits within slashes.




您是否在寻找以下代码?

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;



在上面的代码abc 中会给您想要的结果.

上面的代码摘自

谢谢
-Amit



in above code abc will gives you desired result.

above code is take from this

Thanks
-Amit


用"/"
替换字符串
repalce string with "/"
function string Repalce()
{
    string str="5/33/2012";
    str.replace("/","")
    return str;//You get here "5332012"
}


你要33
然后


you want 33
then

"5332012"
str.substring(1,2)
return str;//You get here 33


这篇关于如何在两个斜杠之间获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆