How to parse back a string.Format output? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file containing data produced by string.Format(), one per line, something like:
Class1 [ Property1 = 123, Property2 = 124 ]
Class2 [ Property4 = 'ABCD', Property5 = 1234, Property6 = 10 ]
Class1 [ Property1 = 2, Property2 = 3 ]
Class2 [ Property4 = 'DEFG', Property5 = 2222, Property6 = 19 ]
and so on...
I need to parse them back to obtain the instances of the classes.
Given that I have the original string.Format template used to produce such lines, what's the fastest way to obtain back the original values so that I can build back the instances of Class1 and Class2 (for fast here I mean in terms developer's time)?
PS: I can rely on the fact that all input strings are "well formed" according to the template.
PPS: I know that using JSON would make this simpler, but right now I can't. Moreover I know Irony too, but I'm looking for something even faster (if possible).

As long as your strings don't contain special characters, here's a start:
var str = "Class1 [ Property1 = 123, Property2 = 124 ]";
var m = Regex.Match(str, #"^(?<name>[^ ]+) \[ ((?<prop>[^ ]+) = (?<val>[^ ,]+),? )+\]$");
Console.WriteLine(m.Groups["name"].Value);
for (var i = 0; i < m.Groups["prop"].Captures.Count; i++)
{
Console.WriteLine(m.Groups["prop"].Captures[i].Value);
Console.WriteLine(m.Groups["val"].Captures[i].Value);
}
Output:
Class1
Property1
123
Property2
124
If your strings do contain special characters, you could go with even more complex regular expressions, or you need to parse your string character by character, in a state machine. The first case I cannot answer because you haven't provided exact rules what your strings can or can't contain. The second case I cannot answer because it's too complex.

Related

get a specific string in C# and trim the value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Hi I would like to trim a string
IWF01 - STSD Campus | 1009432 | Posted Today
I need to get this string 1009432.
Or something like this
ROS03 - Roseville, CA R-3, More... | T_R_1624621 | Posted Today
I want to get this one T_R_1624621.
How do I get that part of string only?
string s = "ROS03 - Roseville, CA R-3, More... | T_R_1624621 | Posted Today";
var strArr = s.Split('|');
string yourValue = strArr[1].Trim();
Beware, this can cause some exceptions. If you don't have right string (that can be splitted by |, or if you have string that has only one | etc...
The first thing I'd do is split the string, then I'd get the 2nd item (provided that there are enough items).
Here's a function that'll do what you need (remember that it won't raise an exception if there aren't enough items):
string GetFieldTrimmed(string input, char separator, int fieldIndex)
{
var strSplitArray = input.Split(separator);
return strSplitArray.Length >= fieldIndex + 1
? strSplitArray[fieldIndex].Trim()
: "";
}
Example usage:
var fieldVal = GetFieldTrimmed("ROS03 - Roseville, CA R-3, More... | T_R_1624621 | Posted Today", '|', 1);

Spliting string with multiple conditions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a string like this -
query = "UserId:(\"787D01FE-D108-4C83-A2E2-4B1DA3166A5C\" OR \"CCA47A4F-B4FA-405C-B34E-EC2E0B1F374C\") AND CreatedDate:[2017-06-20T06:14:11Z TO 2017-07-20T06:14:11Z] OR FirstName: Abc ";
But I want to get the result in array like this -
queries=
{
[0] UserId:(\"787D01FE-D108-4C83-A2E2-4B1DA3166A5C\" OR \"CCA47A4F-B4FA-405C-B34E-EC2E0B1F374C\")
[1] AND
[2] CreatedDate:[2017-06-20T06:14:11Z TO 2017-07-20T06:14:11Z]
[3] OR
[4] FirstName: Abc
}
Updates:
So far I had used this -
var result =
(from Match m in Regex.Matches(query , #"\[[^]]*]|\{[^}]*}|[^:]+")
select m.Value)
.ToArray();
But ended with this -
SOLUTION:
Based on the solution suggested by #NetMage I added some more variations to take care of double quotes, conditions inside parenthesis Here
UserId : ("787D01FE-D108-4C83-A2E2-4B1DA3166A5C" OR "CCA47A4F-B4FA-405C-B34E-EC2E0B1F374C") AND CreatedDate : [ 2017-06-20T06:14:11Z TO 2017-07-20T06:14:11Z ] AND (FirstName : "Abc" OR LastName : "Xyz")
Regex Expression -
(?:\w+?\s*:\s*(\(.+?\)|\".+?\"|\[.+?\]|\w+))|(?:\(\w+?\s*:\s*(\(.+?\)|\".*?\"*|\[.+?\]|\w+)\))|([A-Z]+( [A-Z]+ )?)
How does this work for you?
var pattern = #"(?:\w+? ?: ?(\(.+?\)|\[.+?\]|\w+))|([A-Z]+( [A-Z]+ )?)";
var ans = Regex.Matches(query, pattern).Cast<Match>().Select(m => m.Value).ToArray();

Extracting text from the middle of a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have the following string that is captured from the DVLA when looking up a car registration details and I need to be able to extract just the numbers from the CC.
"A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 -
PRESENT"
Given that the lentgh of the string can change, is there a way to do this with a sub-string so for example always grab the numbers from before the cc without the space that comes before it? Bare in mind too that this can sometimes be a 3 digit number or a four digit number.
This does the trick:
string input = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
string size;
Regex r = new Regex("(\\d*)cc", RegexOptions.IgnoreCase);
Match m = r.Match(input);
if (m.Success)
{
size = m.Groups[0];
}
It captures every number that is right before cc
If the count of the comma doesn't change you can do following:
string s = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
string ccString = s.Split(',').ToList().Find(x => x.EndsWith("cc")).Trim();
int cc = Int32.Parse(ccString.Substring(0, ccString.Length - 2));
You can use Regex to match a pattern withing the string - so you can return parts of the string that match the given pattern. This Regex pattern will attempt to match parts of the string that fit the following pattern:
\d{1,5} *[cC]{2}
Starts with 1 to 5 digits \d{1,5} (seems sensible for an engine cc value!)
Can then contain 0 or more spaces in between that and cc *
Ends with any combination of 2 C or c [cC]{2}
So you can then use this in the following manner:
string str = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
Match result = Regex.Match(str, #"\d{1,5} *[cC]{2}");
string cc = result.Value; // 1968cc
Here is another solution:
string text = "A5 S LINE BLACK EDITION PLUS TDI 190 (2 DOOR), 1968cc, 2015 - PRESENT";
string[] substrings = text.Split(',');
string numeric = new String(substrings[1].Where(Char.IsDigit).ToArray());
Console.WriteLine(numeric);
Here is a working DEMO

c# sort list by input index appearance in the list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my code I get a string as input from the user and return to him a list of names(strings) that consist inside themselves the users input.
I wish to sort this list by the index of the input in the first name and last name, with priority to the first name. example:
User input string: Da
desired result:
"David Gal"
"American Dad"
"Adam Jones"
example explanation:
"David Gal" is first because the index of "Da" is 0 in the first name.
"America Dad" is second because "Da" is also first in the last name but last names have lesser priority than first names when the index is equal.
"Adam Jones" is last because "Da" has the lowest index appearance in his name.
Edit:
I've found the answer but I don't have enough reputation to answer myself so here it is:
listToSort.OrderBy(contact =>
contact.Name
.Split(' ')
.Select((WordInName, WordIndex) =>
(uint)WordInName.IndexOf(SearchString, StringComparison.CurrentCultureIgnoreCase) + WordIndex / ((double)WordIndex + 1)
)
.Min()
);
Assuming your list looks like this:
var input = new List<string>
{
"David Gal",
"American Dad",
"Adam Jones"
};
This will give you a sorted set:
var search = "da";
var results = input
.OrderBy(w =>
w.Split(' ')
.Select(x => x.ToLower().IndexOf(search)).Max());

Parse string with encapsulated int to an array that contains other numbers to be ignored

during my coding I've come across a problem that involved parsing a string like this:
{15} there are 194 red balloons, {26} there are 23 stickers, {40} there are 12 jacks, ....
my code involved pulling both the sentence and the number into two separate arrays.
I've solved the problem involving parsing out the sentence into its own array using a *.Remove(0, 5) to eliminate the first part the problem with that part was that I had to make sure that the file always was written to a standard where {##} where involved however it was not as elegant as I would like in that some times the number would be {3} and i would be forced to make it { 3}.
as there were also the chance of the string containing other numbers I wasn't able to simply parse out the integers first.
int?[] array = y.Split(',')
.Select(z =>
{
int value;
return int.TryParse(z, out value) ? value : (int?)null;
})
.ToArray();
so anyway back to the problem at hand, I need to be able to parse out "{##}" into an array with each having its own element.
Here's one way to do it using positive lookaheads/lookbehinds:
string s = "{15} there are 194 red balloons, {26} there are 23 stickers, {40} there are 12 jacks";
// Match all digits preceded by "{" and followed by "}"
int[] matches = Regex.Matches(s, #"(?<={)\d+(?=})")
.Cast<Match>()
.Select(m => int.Parse(m.Value))
.ToArray();
// Yields [15, 26, 40]

Categories

Resources