Unable to split the string by the dates [duplicate] - c#

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*$)");

Related

Can't get 2 different Substrings from a String C# [duplicate]

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.

fetch sub-string using regex [duplicate]

This question already has answers here:
Regular expression to extract text between square brackets
(15 answers)
Closed 5 years ago.
string subject = "Re: Customer request [#11#]";
I want to fetch [#11#] from the above string. At run time instead of 11 there can be any number e.g. 12,13,14 etc.
Please suggest me appropriate Regex to fetch this output.
Something like this: \[#\d{0,}#\]
You can fiddle with it on https://regex101.com/r/uA8fX9/3

parse selected text to string ignoring chars [duplicate]

This question already has answers here:
Regex to get NUMBER only from String
(3 answers)
Closed 7 years ago.
Im taking a selected.text which can equal 123t, and id like to just take the 123 and insert it into a string. the selected.text can also simply equal 54, or 256, but occasionally it will have a letter. Im unsure of which what to select or parse to remove the letter. i tried to do something convoluted like this as i saw similar questions on SE,
string cat;
int dog;
cat = txtFrame.Text;
dog = int.Parse(cat.Substring(cat.IndexOf("")));
Frame = dog.ToString();
Frame is the string i would like it to end up in.
This is probably best done with Regex
string test = "123t";
Match m = Regex.Match(test, #"\d+");
Console.WriteLine(m.Value);
If an eventual sign is possible in your string then you could use an expanded form of the pattern
Regex.Match(test, #"[-+]?\d+")
From the following post ...
How do you remove all the alphabetic characters from a string?
Regex.Replace(s, "[^0-9.+-]", "")
#Steve's answer is better!
Here's a good resource for RegEx ... http://www.regular-expressions.info/

Splitting these string without clobbering split param's? [duplicate]

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.

How to split a string into multiple strings in c#? [duplicate]

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

Categories