.Net Split with split characters retained [duplicate] - c#

This question already exists:
Closed 10 years ago.
Possible Duplicate:
C# split string but keep split chars / separators
Is there a simple way to do a .Net string split() function that will leave the original split characters in the results?
Such that:
"some text {that|or} another".Split('{','|','}');
would result in an array with:
[0] = "some text "
[1] = "{"
[2] = "that"
[3] = "|"
...
Preferably without a regex.

check out this post
the first answer with a RegEx solution, the second for a non-regex solution...
In Concept...
string source = "123xx456yy789";
foreach (string delimiter in delimiters)
source = source.Replace(delimiter, ";" + delimiter + ";");
string[] parts = source.Split(';');

you can probably roll your own using String.IndexOf Method (String, Int32) to find all of your initial separator characters, and merge those in with the results of String.Split

Related

mvc regex replace array of pattern on one sentence [duplicate]

This question already has answers here:
Why \b does not match word using .net regex
(2 answers)
Closed 5 years ago.
I want to replace certain words in one sentence string with regex replace.
For it, I create pattern array :
string[] words = {"abc","132","qwe","bold","test"};
and for replace, I do it :
foreach (string item in words){
output = Regex.Replace(output,#"\b" + item + "\b", " ");
}
but this way don't work ...
Someone has an idea?
Explanation
I use the above method in VB.net and will respond without problems.
I am a beginner in C #
It looks like you forgot the # literal at the second \b:
either put it in there
output = Regex.Replace(output, #"\b" + item + #"\b", " ");
or double the backslash so it is used as an escape character:
output = Regex.Replace(output, #"\b" + item + "\\b", " ");

getting numbers from a string c# [duplicate]

This question already has answers here:
Using regex to extract multiple numbers from strings
(4 answers)
Closed 5 years ago.
I have an input string like below:
"/myWS/api/Application/IsCarAvailable/123456/2017"
It is the end of a Web API call that I am making. I need to easily extract the 123456 from the URL.
I was hoping something like the below would work
string[] numbers = Regex.Split(input, #"\D+");
However, when I set a breakpoint on numbers and run the code it is showing an array of 3 elements?
Element at [0] is ""
Element at [1] is 123456
Element at [2] is 2017
Does anyone see why it would be getting the empty string as the first element?
I suggest matching, not splitting:
string source = #"/myWS/api/Application/IsCarAvailable/123456/2017";
string[] numbers = Regex
.Matches(source, "[0-9]+")
.OfType<Match>()
.Select(match => match.Value)
.ToArray();
Please, notice that \d+ in .Net means any unicode digits (e.g. Persian ones: ۰۱۲۳۴۵۶۷۸۹): that's why I put [0-9]+ pattern (RegexOptions.ECMAScript is an alternative if \d+ pattern is preferrable).
If your string is always in the same format, this would work:
string numbers = input.Split('/')[input.Split('/').Length - 2];
I think this is because the split method "splits" the string at the matching expression. So the empty string is the part before the first match.
Any reason why you would not use Regex.Matches(input,"\\d+") instead?
string numtest = "http://www.google.com/test/123456/7890";
var matchResult = Regex.Matches(numtest, "\\d+");
for (int i = 0; i < matchResult.Count; i++)
Console.WriteLine($"Element {i} is {matchResult[i].Value}");
Hope that helps. Regards!

splitting the string and choosing the middle part containing two set of parenthesis [duplicate]

This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 7 years ago.
As I know for selecting a part of a string we use split. For example, if node1.Text is test (delete) if we choose delete
string b1 = node1.Text.Split('(')[0];
then that means we have chosen test, But if I want to choose delete from node1.Text how can I do?
Update:
Another question is that when there are two sets of parenthesis in the string, how one could aim at delete?. For example is string is test(2) (delete) - if we choose delete
You can also use regex, and then just remove the parentheses:
resultString = Regex.Match(yourString, #"\((.*?)\)").Value.
Replace("(", "").Replace(")", "");
Or better:
Regex.Match(yourString, #"\((.*?)\)").Groups[1].Value;
If you want to extract multiple strings in parentheses:
List<string> matches = new List<string>();
var result = Regex.Matches(yourString, #"\((.*?)\)");
foreach(Match x in result)
matches.Add(x.Groups[1].Value.ToString());
If your string is always xxx(yyy)zzz format, you can add ) character so split it and get the second item like;
var s = "test (delete) if we choose delete";
string b1 = s.Split(new[] { '(', ')' })[1];
string tmp = node1.Text.Split('(')[1];
string final = tmp.Split(')')[0];
Is also possible.
With the index [x] you target the part of the string before and after the character you have split the original string at. If the character occurs multiple times, your resulting string hat more parts.

Splitting string with dots and spaces [duplicate]

This question already has answers here:
Regex split string but keep separators
(2 answers)
Closed 7 years ago.
Let's say I have the following string: "This is a test. Haha.". I want to split it so that it becomes these there lines:
Hey.
This is a test.
Haha.
(Note that the space after the dot is preserved).
I tried to split the string using the Split method, and split by the dot, but it returns 3 new strings with the space before the string, and it removes the dots. I want to keep the space after the dot and keep the space.
How can I achieve this?
EDIT: I found a workaround, but I'm sure there's a simpler way:
string a = "Hey. This is a test. Haha.";
string[] splitted = a.Split('.');
foreach(string b in splitted)
{
if (b.Length < 3)
{
continue;
}
string f = b.Remove(0, 1);
Console.WriteLine(f + ". ");
}
I can't test this but due to the post of Darin Dimitrov :
string input = "Hey. This is a test. Haha.";
string result = input.Replace(". ", ".\n");

Remove characters between different parameters [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I have a string of different emails
ex: "email1#uy.com, email2#iu.it, email3#uu.edu" etc, etc
I would like to formulate a Regex that creates the following output
ex: "email1,email2,email3" etc, etc
How can I remove characters between an "#" and "," but leaving a "," and a Space in C#
Thank you so much for the help!!
If you want to replace all characters between # and comma by blank, the easiest option is to use Regex.Replace:
var emails = "a#m.com, b#m.com, d#m.com";
var result = Regex.Replace(emails, "#[^,]+", string.Empty);
// result is "a, b, d"
Please note that it leaves spaces after comma in the result, as you wanted in your question, though your example result has spaces removed.
The regular expression looks for all substrings starting '#' characters, followed by any character which is not comma. Those substrings are replaced with empty string.
Replacing all occurrences of #[^,]+ with an empty string will do the job.
The expression matches sequences that start in #, inclusive, up to a comma or to the end, exclusive. Therefore, commas in the original string of e-mails would be kept.
Demo.
Maybe you don't need to use a regex, in that case you can do the following:
string input = "email1#uy.com, email2#iu.it, email3#uu.edu";
input = input.Replace(" ", "");
string[] ocurrences = input.Split(',');
for (int i = 0; i < ocurrences.Length; i++)
{
string s = ocurrences[i];
ocurrences[i] = s.Substring(0, s.IndexOf('#'));
}
string final = string.Join(", ", occurences);

Categories

Resources