C# RegEx string extraction

前端 未结 5 677
孤街浪徒
孤街浪徒 2020-12-05 06:06

I have a string:

\"ImageDimension=655x0;ThumbnailDimension=0x0\".

I have to extract first number (\"655\" string) coming in be

相关标签:
5条回答
  • 2020-12-05 06:45

    This will get each of the values into separate ints for you:

    string text = "ImageDimension=655x0;ThumbnailDimension=0x0";
    Regex pattern = new Regex(@"ImageDimension=(?<imageWidth>\d+)x(?<imageHeight>\d+);ThumbnailDimension=(?<thumbWidth>\d+)x(?<thumbHeight>\d+)");
    Match match = pattern.Match(text);
    int imageWidth = int.Parse(match.Groups["imageWidth"].Value);
    int imageHeight = int.Parse(match.Groups["imageHeight"].Value);
    int thumbWidth = int.Parse(match.Groups["thumbWidth"].Value);
    int thumbHeight = int.Parse(match.Groups["thumbHeight"].Value);
    
    0 讨论(0)
  • 2020-12-05 06:47
    var m = Regex.Match(str,@"(\d+).(\d+).*?(\d+).(\d+)");
    m.Groups[1].Value; // 655 ....
    

    (\d+) 
    

    Get the first set of one or more digits. and store it as the first captured group after the entire match

    .
    

    Match any character

    (\d+)
    

    Get the next set of one or more digits. and store it as the second captured group after the entire match

    .*? 
    

    match and number of any characters in a non greedy fashion.

    (\d+)
    

    Get the next set of one or more digits. and store it as the third captured group after the entire match

    (\d+)
    

    Get the next set of one or more digits. and store it as the fourth captured group after the entire match

    0 讨论(0)
  • 2020-12-05 06:48
    var groups = Regex.Match(input,@"ImageDimension=(\d+)x(\d+);ThumbnailDimension=(\d+)x(\d+)").Groups;
    var x1= groups[1].Value;
    var y1= groups[2].Value;
    var x2= groups[3].Value;
    var y2= groups[4].Value;
    
    0 讨论(0)
  • 2020-12-05 06:51

    Since a lot of people already gave you what you wanted, I will contribute with something else. Regexes are hard to read and error prone. Maybe a little less verbose than your implementation but more straightforward and friendly than using regex:

    private static Dictionary<string, string> _extractDictionary(string str)
    {
        var query = from name_value in str.Split(';')   // Split by ;
                    let arr = name_value.Split('=')     // ... then by =
                    select new {Name = arr[0], Value = arr[1]};
    
        return query.ToDictionary(x => x.Name, y => y.Value);
    }
    
    public static void Main()
    {
        var str = "ImageDimension=655x0;ThumbnailDimension=0x0";
        var dic = _extractDictionary(str);
    
        foreach (var key_value in dic)
        {
            var key = key_value.Key;
            var value = key_value.Value;
            Console.WriteLine("Value of {0} is {1}.", key, value.Substring(0, value.IndexOf("x")));
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:59

    Sure, it's pretty easy. The regex pattern you're looking for is:

    ^ImageDimension=(\d+)x0;.+$
    

    The first group in the match is the number you want.

    0 讨论(0)
提交回复
热议问题