C#RegEx字符串提取

mis*_*hap 56 c# regex string

我有一个字符串:

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

我必须提取"ImageDimension ="和第一次出现"x"之间的第一个数字("655"字符串); 并且需要在"ImageDimension ="字符串之后的第一个"x"之后提取第二个数字("0"字符串).与第三和第四个数字相似.

这可以用正则表达式完成("ImageDimension = x ? ; ThumbnailDimension = x ")以及如何完成?而不是笨拙的子串和索引?谢谢!

我的解决方案不太好:

String configuration = "ImageDimension=655x0;ThumbnailDimension=0x0";
String imageDim = configuration.Substring(0, configuration.IndexOf(";"));
int indexOfEq = imageDim.IndexOf("=");
int indexOfX = imageDim.IndexOf("x");

String width1 = imageDim.Substring(indexOfEq+1, indexOfX-indexOfEq-1);
String height1 = imageDim.Substring(imageDim.IndexOf("x") + 1);

String thumbDim = configuration.Substring(configuration.IndexOf(";") + 1);
indexOfEq = thumbDim.IndexOf("=");
indexOfX = thumbDim.IndexOf("x");

String width2 = imageDim.Substring(indexOfEq + 1, indexOfX - indexOfEq-1);
String height2 = imageDim.Substring(imageDim.IndexOf("x") + 1);
Run Code Online (Sandbox Code Playgroud)

its*_*e86 100

这将使每个值成为单独的int:

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);
Run Code Online (Sandbox Code Playgroud)

  • 您还可以使用以下模式使搜索更通用:`@"(?<imageWidth>\d +).(?<imageHeight>\d +).*?(?<thumbWidth>\d +).(?< thumbHeight>\d +)"` (2认同)

L.B*_*L.B 14

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;
Run Code Online (Sandbox Code Playgroud)


rer*_*run 9

var m = Regex.Match(str,@"(\d+).(\d+).*?(\d+).(\d+)");
m.Groups[1].Value; // 655 ....
Run Code Online (Sandbox Code Playgroud)
(\d+) 
Run Code Online (Sandbox Code Playgroud)

获取第一组一个或多个数字.并在整场比赛后将其存储为第一个被捕获的组

.
Run Code Online (Sandbox Code Playgroud)

匹配任何角色

(\d+)
Run Code Online (Sandbox Code Playgroud)

获取下一组一个或多个数字.并在整场比赛后将其存储为第二个被捕获的组

.*? 
Run Code Online (Sandbox Code Playgroud)

以非贪婪的方式匹配和任何字符的数量.

(\d+)
Run Code Online (Sandbox Code Playgroud)

获取下一组一个或多个数字.并在整场比赛后将其存储为第三个被捕获的组

(\d+)
Run Code Online (Sandbox Code Playgroud)

获取下一组一个或多个数字.并在整场比赛后将其存储为第四个被捕获的组


Bru*_*ant 5

既然很多人已经给了你你想要的东西,我会贡献一些别的东西。正则表达式难以阅读且容易出错。也许比你的实现少一点冗长,但比使用正则表达式更直接和友好:

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")));
    }
}
Run Code Online (Sandbox Code Playgroud)