C# RegEx 字符串提取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9436381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# RegEx string extraction
提问by mishap
I have a string:
我有一个字符串:
"ImageDimension=655x0;ThumbnailDimension=0x0".
“ImageDimension=655x0;ThumbnailDimension=0x0”。
I have to extract first number ("655" string) coming in between "ImageDimension=" and first occurrence of "x" ; and need extract second number ("0" string) coming after first "x" occurring after "ImageDimension=" string. Similar with third and fourth numbers.
我必须提取介于 "ImageDimension=" 和第一次出现 "x" 之间的第一个数字(“655”字符串);并且需要提取出现在“ImageDimension=”字符串之后的第一个“x”之后的第二个数字(“0”字符串)。与第三和第四个数字类似。
Can this be done with regex ("ImageDimension=?x ?;ThumbnailDimension=?x ?") and how ? Instead of clumsy substrings and indexof ? Thank you!
这可以用 regex ("ImageDimension= ?x ?;ThumbnailDimension= ?x ?")来完成吗?怎么做?而不是笨拙的子字符串和 indexof ?谢谢!
My solution which is not nice :
我的解决方案不好:
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);
采纳答案by itsme86
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);
回答by rerun
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
获取下一组一个或多个数字。并将其存储为整个比赛后的第四个捕获组
回答by Stephen Gross
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.
比赛中的第一组是您想要的号码。
回答by L.B
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;
回答by Bruno Brant
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")));
}
}

