This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 4 years ago.
I have a task.
I should extract two substrings from a string.
The lenght of the String will be different each time, so the method should be generic.
Suppose I have the following String:
/*Description:\r\n*RANGE:\r\n*HIGH\r\n*LOW\r\n*/
I need to get the substring1= HIGH and substring2= LOW.
The substring1 and substring2 will be all the time between \r\n, but they values will be different.
I would be very grateful if anybody helps me. It can be a pseudocode, anything.
Thanks in advance.
UPDATE1: I'm searching first for "RANGE:\r\n*" and get the index of the character * and the index og character "H". But next don't know how to get the whole substring.
If the pattern you've provided is similar to what you'd expect all the time, a stupid simple approach would be:
public static string[] GetParts(string input)
{
string[] parts= input.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
return parts.TakeLast(2).Select(item=>item.Replace("*", string.Empty)).ToArray();
}
Note: This is not a production quality code.
Related
This question already has an answer here:
Regex to match any combination and number of whitespaces and linebreaks between groups
(1 answer)
Closed 2 months ago.
I do have search pattern for Regex, but my multiple file have different number of spaces between them. So I need to ignore them in my pattern.
const string PATTERN = #"OTPM = true";
Can someone modify this line for me? I tried different solutions which I found here, but didnt work, since I am a bit new to C#
OTPM\s+= true
Worked perfectly fine. Thank you very much, for all responses
This question already has an answer here:
Regex to split a string but keep delimiters, but not as separate elements
(1 answer)
Closed 4 years ago.
I am trying to split the text right after each dates. I was trying with the below code. But it is give me the string itself.
string[] split = Regex.Split("SEND MILK EVERYDAY FOR THIS PERSON FROM 02/10/2014 TO 02/11/2014 SKIP 03/11/2014 AND 09/11/2014", #"^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$")
Looking for a output like
SEND MILK EVERYDAY FOR THIS PERSON FROM 02/10/2014
TO 02/11/2014
SKIP 03/11/2014
AND 09/11/2014
You may try this regex to split,
(?<=\d{4})\s+
Demo,,, in which you can see split points
This worked for me.
string[] split = Regex.Split("SEND MILK EVERYDAY FOR THIS PERSON FROM 02/10/2014 TO 02/11/2014 SKIP 03/11/2014 AND 09/11/2014", #"(?<=\b(?:0?[1-9]|[12][0-9]|3[01])/-[/-]\d{4}\b)\s*(?!\s*$)");
This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 7 years ago.
i have a semicolon separates string, that contains values of every type. string and date values are in quotations.
Now i have an evil string, where an inner string contains s semicolon, that i need to remove (replace by nothing).
eg:
"Value1";0;"Value2";4711;"Evil; Value";"2015-09-03"
in C#:
string value = "\"Value1\";0;\"Value2\";4711;\"Evil; Value\";\"2015-09-03\""
So how to replace all semicolons, that are in quotations? can anybody help?
Regex is awful at handling delimited strings. It can do it, but it's not often as good of a choice as it first appears. This is one of several reasons why.
Instead, you should use a dedicated delimited string parser. There are (at least) three built into the .Net framework. The TextFieldParser type is one of those, and it will handle this correctly.
You should try this i.e to match only those semicolons which is not preceded by : :
(?<=[^"]);
Here is demo
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to split a string while preserving line endings?
How do I split a string by strings and include the delimiters using .NET?
I'm splitting text into sentences. mystring.Split('.','!', '?') returns the sentences without the ./!/? on them. I need to have it return a sentence with the split param on the end? How does that go? Thanks
public static string[] GetSentences(string text)
{
return text.Split('.', '!', '?');
}
I can think of one way to do it, by combining two separate arrays, but I think it looks awful so I thought I'd ask you professionals for a "proper" way :D
Edit - never mind close its a duplicate. I found the other threads, sorry
Right, string.Split() isn't the right tool here.
Either simply loop through it (string.IndexOf())
or use a RegEx: ([^\.!?]+[\.!?])*
I'm not 100% sure about the escaping.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Split String into smaller Strings by length variable
I have seen solutions that split strings by a certain character but I wanted to seperate a string every certain amount of characters. Does anyone know how to do this?
its been asked
Split String into smaller Strings by length variable