C#正则表达式字符串提取 [英] C# RegEx string extraction
问题描述
我有一个字符串:
ImageDimension = 655x0; ThumbnailDimension =为0x0。
块引用>
我要提取第一个数字(655字符串)之间的进来ImageDimension =和X第一次出现;
和需要提取第二数字(0串)之后的第一个×来后ImageDimension =串的发生。相似与第三个和第四个数字
可以这样用正则表达式完成(ImageDimension = X ?; ThumbnailDimension = ? X ?)又如何?而不是笨拙子和的indexOf? !谢谢
我的解决方案,它是不是很好:
字符串配置=ImageDimension = 655x0; ThumbnailDimension =为0x0;
字符串imageDim = configuration.Substring(0,configuration.IndexOf(;));
INT indexOfEq = imageDim.IndexOf(=);
INT indexOfX = imageDim.IndexOf(×);
字符串宽度1 = imageDim.Substring(indexOfEq + 1,indexOfX-indexOfEq-1);
字符串height1 = imageDim.Substring(imageDim.IndexOf(X)+ 1);
字符串thumbDim = configuration.Substring(configuration.IndexOf(;)+ 1);
indexOfEq = thumbDim.IndexOf(=);
indexOfX = thumbDim.IndexOf(×);
字符串宽度2 = imageDim.Substring(indexOfEq + 1,indexOfX - indexOfEq-1);
字符串身高2 = imageDim.Substring(imageDim.IndexOf(X)+ 1);
解决方案这将得到的每个值转化为独立的整数你:
字符串文本=ImageDimension = 655x0; ThumbnailDimension =为0x0;
正则表达式=新的正则表达式(@ImageDimension =(小于?imageWidth> \d +)×(< imageHeight> \d +); ThumbnailDimension =(小于?thumbWidth> \d +)×( ?&所述; thumbHeight> \d +));
匹配匹配= pattern.Match(文本);
INT imageWidth = int.Parse(match.Groups [imageWidth]值。);
INT imageHeight = int.Parse(match.Groups [imageHeight]值。);
INT thumbWidth = int.Parse(match.Groups [thumbWidth]值。);
INT thumbHeight = int.Parse(match.Groups [thumbHeight]值。);
I have a string:
"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.
Can this be done with regex ("ImageDimension=? x ?;ThumbnailDimension=? x ?") and how ? Instead of clumsy substrings and indexof ? Thank you!
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);
解决方案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);
这篇关于C#正则表达式字符串提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!