Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# RegEx string extraction

Tags:

string

c#

regex

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);
like image 461
mishap Avatar asked Feb 24 '12 18:02

mishap


3 Answers

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);
like image 133
itsme86 Avatar answered Nov 10 '22 07:11

itsme86


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;
like image 20
L.B Avatar answered Nov 10 '22 09:11

L.B


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

like image 12
rerun Avatar answered Nov 10 '22 09:11

rerun