|
But how does RegEx know where the value ends and the next key starts?
If it's not broken, fix it until it is
|
|
|
|
|
It just does. Read up on "Regular Expressions" first.
RegEx doesn't just look at a string from one end work one character at a time to the other end. It's a pattern matching library that does a really good job at string manipulations.
|
|
|
|
|
That's what the pattern does. It's looking for single words terminated by : to create the Key. Anything else is the Value.
|
|
|
|
|
Thanks. I could use some help with this. Never done RegEx, so I'm totally unsure how to code it.
So far I have:
var expression = @"(?:(?'Key'\S+): (?'Value'.*?)) ";
Match match = Regex.Match(Line, expression,RegexOptions.IgnoreCase);
if (match.Success)
{
string key = match.Groups[1].Value;
}
I have the key, but how do I get the value out?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Kevin Marois wrote: Never done RegEx, so I'm totally unsure how to code it.
In any spare time you may have, it may be worth your while to learn how to use regular expressions and work with them. It's a powerful skill to have in your skill set
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
modified 19-Jun-12 18:34pm.
|
|
|
|
|
To add to what Paul said, you also need to install this[^].
|
|
|
|
|
|
You can stop looking at mine. I wondered why there were fingerprints on the outside of the bedroom window.
|
|
|
|
|
Those aren't finger prints.
|
|
|
|
|
*Mind bleach* OK, I'm going to have to move house now.
|
|
|
|
|
with the RightToLeft option.
|
|
|
|
|
Your regex should actually look like this:
public static Regex regex = new Regex(
"(?:(?'Key'\\S+): (?'Value'.*?))",
RegexOptions.RightToLeft
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
|
|
|
|
|
Ok, so how so I call it? This doesn't compile:
regex.Match(Line, expression);
I don't understand RegEx at all... It's all greek to me.
If it's not broken, fix it until it is
|
|
|
|
|
Sorry Kevin - you posted after I logged off for the night. Anyway, you don't want to use Match , you want to use Matches because you are returning a collection:
MatchCollection matches = regex.Matches(expression);
foreach (Match match in matches)
{
Console.WriteLine("Key: {0} - Value: {1}", match.Groups["Key"], match.Groups["Value"]);
} Basically, you have returned a collection of matches here. In the expression, you created two named capture groups so you need to reference the items by name by looking in the Groups property.
|
|
|
|
|
[EDIT] - Never mind. I'm stupid. I changed it to...
MatchCollection matches = regex.Matches(Line);
Now it works.
Thanks for the code. I'm still getting nothing in the collection. Here's my code:
var Line = "Dev: 0 Model: TOSHIBA MK3265GSX Serial: 20FDF20WS FW: GJ002H STW: 0 MaxLBA: 625142447 FDESUPPORTED: 0 PREBOOT: 0 DRIVETRUSTENABLED: 0 DRIVETRUSTSUPPORTED: 0 w128: 41 FULLFW: GJ002H SERVOFW: SDLSUPPORTED: 1 PLATFORM: 0 SAFE: 1 DSTTIMEOUT: 103 ISBOOTORSYSTEM: 1";
var WordToFind = "Model";
Regex regex = new Regex("(?:(?'Key'\\S+): (?'Value'.*?))",
RegexOptions.RightToLeft |
RegexOptions.CultureInvariant |
RegexOptions.Compiled);
MatchCollection matches = regex.Matches(WordToFind);
foreach (Match match in matches)
{
Console.WriteLine("Key: {0} - Value: {1}", match.Groups["Key"], match.Groups["Value"]);
}
I'm guessing the regex string is wrong, but kI really don't knnow.
If it's not broken, fix it until it is
|
|
|
|
|
Kevin Marois wrote: EDIT] - Never mind. I'm stupid. I changed it to...
MatchCollection matches = regex.Matches(Line);
OK, I was just about to point that out - good job.
|
|
|
|
|
Thanks for your help.
If it's not broken, fix it until it is
|
|
|
|
|
No problem. I'm glad we could help.
|
|
|
|
|
with the RightToLeft option it knows where the key ends and the previous value starts.
modified 19-Jun-12 18:44pm.
|
|
|
|
|
I want to programmatically create Excel charts in my c# application. Is there a way I can get the chart into a Panel control in my application? I do not want the Excel grid or cells to be part, just the chart output that is inserted in the worksheet. Thanks.
|
|
|
|
|
Dan_K wrote: I do not want the Excel grid or cells to be part, just the chart output that is inserted in the worksheet. Thanks.
A lot of those formula's are calculated at runtime, and are not exactly "inserted" in the physical file.
We got quite some articles on reading Excel-files and getting their content - check them out.
Bastard Programmer from Hell
|
|
|
|
|
Any links to resources that describe that? I will be very much happy with that.
|
|
|
|
|
On of the articles on the subject can be found here[^].
Bastard Programmer from Hell
|
|
|
|
|
Thanks Eddy, I think the charts are inserted in the worksheet. I don't know if there is a way to send only the chart into my application. Any way to do that?
|
|
|
|
|
What do you mean with "send into my application"?
Excel does not store a chart, but a description of a chart - one that's usually based on values within the same document. You can probably copy the contents to a bitmap and store that in your database.
Bastard Programmer from Hell
|
|
|
|