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.
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 answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.
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.
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:
How to use a string with quotation marks inside it?
(7 answers)
Closed 7 years ago.
I'm trying to make my program press the " (Shift + 2 in the UK) when a button is pressed, but I'm not sure how to as when I enter..
SendKeys.Send(""");
It converts the latter part of the code to string.
Is there a way to make the program ignore a particular character? I'm not really sure how to go about this.
I'm making an on screen keyboard if anyone is wondering.
Escape the ", like so:
SendKeys.Send("\"");
Or use a string literal:
SendKeys.Send(#"""");
String literals in C# are usually nicer to read, but in this case, you still need to escape the single quote in the string literal, by doubling it.
In fact this is an interesting example of the one approach vs. the other:
"\"" // 4 chars, not very readable
vs:
#""""" // 5 chars: even LESS readable: pretty confusing what it means
SendKeys.Send("\"");
Something like this should work.
Have a look at this (what you're looking for is called escaping characters)
You can use escape characters:
SendKeys.Send("\"");
The \" translates to just ".