当前位置:编程学习 > C#/ASP.NET >>

求一正则表达式

有个字符串,包含有{N4},其中数字会变,现在想实现,判断是否包含该字符子串,且取出其中的数字4,请问该如何取? --------------------编程问答--------------------

string str = string.Empty;
str.Contains("{N4}");就是判断是否包含。
--------------------编程问答-------------------- 如果只是取4的话,直接把需要赋值4的地方加4就行。不用在截取。 --------------------编程问答-------------------- private string[] GetCode()
        {
            string strspring;
            string strmuiao;
            List<string> lt = new List<string>();
            strspring = "ggghhggN8";
            for (int i = 0; i < 9; i++)
            {
                strmuiao = "N";
                strmuiao += i.ToString();
                int index = strspring.IndexOf(strmuiao);
                if (index > 0)
                {
                    string str = strspring.Substring(index, 2);
                    lt.Add(str);
                }
            }
            return lt;
        }
你看这样行不,代码有些不规范,主要是功能,可以的话记得给分哦! --------------------编程问答-------------------- private int GetCode()
        {
            string strspring;
            string strmuiao;
            List<string> lt = new List<string>();
            strspring = "ggghhggN8";
            for (int i = 0; i < 9; i++)
            {
                strmuiao = "N";
                strmuiao += i.ToString();
                int index = strspring.IndexOf(strmuiao);
                if (index > 0)
                {
                    string str = strspring.Substring(index, 2);
                    lt.Add(str);
                }
                strmuiao = "";
            }
            int n = Convert.ToInt32(lt[1]);
        }
这个就比较完整了,拿分来,呵呵! --------------------编程问答-------------------- Regex regEx = new Regex( @"\{N(?<num>\d+)\}", RegexOptions.IgnoreCase );
Match m = regEx.Match( 要被查找的字符串 );
if ( m.Success )
{// 找到
m.Groups["num"].Value 这个就是你要找的数字,如果这个数字是多位也能被找出来
} --------------------编程问答-------------------- 上面的那个?和<之间多了个空格 --------------------编程问答-------------------- (\x7BN)\d\x(7D)
然后取第一个和第二个匹配的,中间的那个数字自己替换
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,