Regex matches the string

Regex document path: http://www.runoob.com/regexp/regexp-syntax.html

Regex online tools path : https://regex101.com/

1.Using directive 

using System.Text.RegularExpressions;

2.You need to define Regex methods : Regex reg = new Regex(); . And then you can matches between string and regex exprossion . In the end , you can see the metch string in the match value.

 static void Main(string[] args)
        {
            string s = "one{Temps forts antérieurs de {displayName}.}";
            //regex
            string regex = @"one{.*?([^{])*\}";
            Regex reg = new Regex(regex);
            Match match = reg.Match(s);
            string value = match.Value;
        }
View Code

3.Result :The value is not empty , proved match to succeed .

4. Regular expression matching text, it gets more value .

 /// <summary>
        /// match more regex foreach get value
        /// </summary>
        public static void RegexCheck()
        {
            string str = "{count, plural, =0{Older highlights from {displayName}.} =1{1 new highlight from {displayName}.} otherd{{# new other highlights from {displayName}.}}";
            string reg = @"(other | =1)*?{.*?([^{])*\}";
            Regex regex = new Regex(reg, RegexOptions.IgnoreCase | RegexOptions.Multiline);
            if (regex.IsMatch(str))
            {
                MatchCollection matchCollection = regex.Matches(str);
                foreach (Match match in matchCollection)
                {
                    string value = match.Value.Trim();
                }
            }
        }
View Code

 

posted @ 2017-08-11 14:52  Yanzhiyi  阅读(195)  评论(0)    收藏  举报