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 2 years ago.
Improve this question
I have a string that has key-value pair as comma-separated and I want to split the string into key-value. I have a problem in splitting if the value has a comma.
Below is the sample string
job=123,Auto=Workflow1,Workflow2,debug=true
and I want to split the above string into key-value as below
job=123
Auto=Workflow1,Workflow2
debug=true
How can I do this?
We can try splitting on the following regex pattern:
,(?=[^,]+=)
This will match any comma for which we can find an = following without crossing over another comma. This rules out the in between commas which we don't want to split.
Sample code:
string input = "job=123,Auto=Workflow1,Workflow2,debug=true";
string[] parts = Regex.Split(input, #",(?=[^,]+=)");
foreach (string part in parts)
Console.WriteLine(part);
This prints:
job=123
Auto=Workflow1,Workflow2
debug=true
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to find between 2 words and replace them into new string?
I wanna replace same.
for example given string :
string s = "word1-word2same-word3-word4";
same is = "word2";
What i wanna :
string s = "word1-word2word2-word3-word4";
But what if given string is:
string s = "word1same-word2-word3-word4";
same is -> word1;
What i wanna in this example:
string s = "word1word1-word2-word3-word4";
How to find what word is containing same? How to do it?
I think the following regular expression replacement is what you want
var input = "word1same-word2same-word3";
var result = Regex.Replace(input, "(?<=^|-)([^-]*)same", "$1$1");
Console.WriteLine(result);
would give you
word1word1-word2word2-word3
So it will replace all the instances of "same" with whatever comes before it up to a hyphen or the beginning of the string.
Note that something like "whatsamesamesame" will give you a result of "whatsamesamewhatsamesame", basically it will only replace the last "same" after a hyphen or the beginning of the string.
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 3 years ago.
Improve this question
As the title indicates, how can I pass an array of delimiters to a Regex.Split function? I'm needing to split a sentence (string) by a list of words (also strings).
You can build a regex pattern from "delimiter" words like this:
var delim = new string[] {"fox", "lazy"};
var pattern = #"(?:\s|^)(?:" + string.Join("|", delim.Select(Regex.Escape)) + #")(?:\s|$)";
\s and string anchors at the beginning and at the end ensure that delimiters include all white space around them, and that you avoid the Scunthorpe problem. Using Regex.Espace ensures that delimiters with regex meta-characters do not break your code.
The resultant pattern looks as follows:
(?:\s|^)(?:fox|lazy)(?:\s|$)
Demo 1
If you would like to keep delimiter words among the tokens, change regex to use them in a lookahead/lookbehind:
var delimGroup = "(?:"+string.Join("|", delim.Select(Regex.Escape))+")";
var pattern = #"\s(?="+delimGroup+")|(?<="+delimGroup+#")\s";
Demo 2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to found and replace what is in a quotes in the string?
String for search and replace is
#define IP "127.0.0.1"
First I read all text from the file
string text = File.ReadAllText(path);
Than how to replace the what is in a quotes in the string?
Thanks.
Try using Regex.Replace. Example:
var input = "#define IP \"127.0.0.1\"";
var replacement = "4.4.4.4";
Regex rgx = new Regex(#"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
string result = rgx.Replace(input, replacement);
// result: #define IP "4.4.4.4"
The regular expression \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} will match any IP. I've created an example on dotnetfiddle for you to see it matching 3 different IP Addresses.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is it possible to concatenate two sub strings from a input string using Regex
Example : Input string "ABTTPQR 00100300250000" and I want take first two characters "AB" and first 9 digits "001003002" and concatenate these two string to one "AB001003002"
Much shorter variation using references:
Regex.Replace("ABTTPQR 00100300250000", #"^(\w{2})\w*\s(\d{9})\d+$", #"$1$2")
// = "AB001003002"
You can do as follows :
Regex regex = new Regex(#"(\w{2})\w*\s(\d{9})\d*");
var matches = regex.Matches("ABTTPQR 00100300250000");
String output = matches[0].Groups[1].Value + matches[0].Groups[2].Value;
//AB001003002
Regex Explanation:
(\w{2})\w*\s(\d{9})\d*
The (\w{2}) will match the first 2 chars, then it will look for more chars and a space \w*\s then it will match first 9 digits (\d{9}) and then will look for more digits \d*.
The () captures the matches into groups which are accessed in c# like shown in code sample: matches[0].Groups[1].Value for group 1 (chars) and matches[0].Groups[2].Value for group 2 (digits)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to split the following string based on the commas that come before the "or" delimiter and on the "or", but not after. For example
Almondmilk, Coconutmilk or Soymilk Select Varieties, Half Gallon
becomes
Almondmilk
Coconutmilk
Soymilk Select Varieties, Half Gallon
Given your requirement as described:
var output = Regex.Split(input, "(?<!or.*),");
However, given your sample output it seems you want to split on 'or' as well:
var output = Regex.Split(input,
"((?<!or.*),)|(or)",
RegexOptions.ExplicitCapture);