Regex replace semicolon between quotation marks C# [duplicate] - c#

This question already has answers here:
How to remove all commas that are inside quotes (") with C# and regex
(3 answers)
Closed 3 years ago.
I want to replace all semicolons that are enclosed in quotation marks with a space. How can I do this in C#?
For example:
this string:
this is an example; "this is other ; example"
Would return:
this is an example; "this is other example"
I await your help!

Edited: This will work.
string yourString = "hello there; \"this should ; be replaced \"";
string fixedString = Regex.Replace(yourString, "(\"[^\",]+);([^\"]+\")", delegate (Match match)
{
string v = match.ToString();
return v.Replace(";", " ");
});

Try following :
string input = "this is an example; \"this is other ; example\"";
string pattern = "\"(?'prefix'[^;]+);(?'suffix'[^\"]+)\"";
string output = Regex.Replace(input,pattern,"\"${prefix} ${suffix}\"");

Related

How to strip only the "\n" character that is inside double quotes using regex [duplicate]

This question already has answers here:
Replace all occurrences of the Tab character within double quotes
(2 answers)
Regex replace enters between quotes
(2 answers)
Closed 3 years ago.
The text patten would be like this
\n "this is some text \n this should not be on the new line"
I want to detect only the \n that is inside the double quotes and have it stripped out.
So the end result would be
\n "this is some text this should not be on the new line"
Try something like this fiddle
using System;
using System.Text.RegularExpressions;
public class Simple
{
static string StripLinefeeds(Match m)
{
string x = m.ToString(); // Get the matched string.
return x.Replace("\n", "");
}
public static void Main()
{
Regex re = new Regex(#""".+?""", RegexOptions.Singleline);
MatchEvaluator me = new MatchEvaluator(StripLinefeeds);
string text = "\n\"this is some text \n this should not be on the new line\"";
Console.WriteLine(text);
text = re.Replace(text, me);
Console.WriteLine(text);
text = "\n\"this is some \n text \n\n\n this should not be \n\n on the new line\"";
Console.WriteLine(text);
text = re.Replace(text, me);
Console.WriteLine(text);
}
}
This pattern might work for you.
/(?<=(\"(.|\n)*))(\n(?=((.|\n)*\")))/g
This is a "look-behind" (?<=(\"(.|\n)*))(...) and a "look-ahead" \n(?=((.|\n)*\")) paired together. It will only be reliable with one pair of quotes at most though, so if your needs change, you will have to adjust for that.
I think this would be the simplest way:
Match ("\w.[^\\]+)\\n ?
And replace with \1
https://regex101.com/r/8UXMzZ/3

RegEx in C# Replace Method [duplicate]

This question already has answers here:
C#: How to Delete the matching substring between 2 strings?
(6 answers)
Closed 4 years ago.
I am trying to write the RegEx for replacing "name" part in below string.
\profile\name\details
Where name: -Can have special characters
-No spaces
Let's say I want to replace "name" in above path with ABCD, the result would be
\profile\ABCD\details
What would be the RegEx to be used in Replace for this?
I have tried [a-zA-Z0-9##$%&*+\-_(),+':;?.,!\[\]\s\\/]+$ but it's not working.
As your dynamic part is surrounded by two static part you can use them to find it.
\\profile\\(.*)\\details
Now if you want to replace only the middle part you can either use LookAround.
string pattern = #"(?<=\\profile\\).*(?=\\details)";
string substitution = #"titi";
string input = #"\profile\name\details
\profile\name\details
";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
Or use the replacement patterns $GroupIndex
string pattern = #"(\\profile\\)(.*)(\\details)";
string substitution = #"$1Replacement$3";
string input = #"\profile\name\details
\profile\name\details
";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
For readable nammed group substitution is a possibility.

Split a string after reading square brackets in c# [duplicate]

This question already has answers here:
splitting a string based on multiple char delimiters
(7 answers)
Closed 5 years ago.
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string new = test.Trim("[");
I want output "AccoutNumber".
I have tried the below code but not getting the desired result:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[');
string newvalue = test[1];
Just use Split with two delimiters:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[', ']');
string newvalue = test[1];
You can also use Regex:
string test = "Account.Parameters[\"AccountNumber\"].Caption";
var match = System.Text.RegularExpressions.Regex.Match(test, ".*?\\.Parameters\\[\"(.*?)\"]");
if (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
}
.*? is a non greedy wildcart capture, so it will match your string until it reaches the next part (in our case, it will stop at .Parameters[", match the string, and then at "])
It will match .Parameters["..."]., and extract the "..." part.
you can do a Split to that string...
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string output = test.Split('[', ']')[1];
Console.WriteLine(output);

What is the proper way to split a string by regex [duplicate]

This question already has answers here:
How to keep the delimiters of Regex.Split?
(6 answers)
Closed 6 years ago.
I have a text string with a custom macro:
"Some text {MACRO(parameter1)} more text {MACRO(parameter2)}"
In order to process the macro I want to split the string like that:
expected result
"Some text "
"{MACRO(parameter1)}"
" more text "
"{MACRO(parameter2)}"
I've tried to split the string using Regex.Split()
public static string[] Split(string input)
{
var regex = new Regex(#"{MACRO\((.*)\)}");
var lines = regex.Split(input)
return lines;
}
However Regex.Split() deletes the match itself and gives me the following:
actual result
"Some text "
" more text "
I know I could parse the string doing iterations of .Match() and .Substring()
But is there an easy way get the result along with the matches?
Try this
string input = "Some text {MACRO(parameter1)} more text {MACRO(parameter2)}";
string pattern = #"(?'text'[^\{]+)\{(?'macro'[^\}]+)\}";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine("text : '{0}'; macro : '{1}'", match.Groups["text"].Value.Trim(), match.Groups["macro"].Value.Trim());
}
Console.ReadLine();

Replace a set of characters in a string [duplicate]

This question already has answers here:
RegEx Starts with [ and ending with ]
(4 answers)
Closed 6 years ago.
How to replace a set of characters where I only know the first and the last one, in between is a variable that is not constant.
All I know is that this string will always start with & and it will end with ;
string str = "Hello &145126451; mate!";
How to get rid of &145126451; ?
So the desired result is:
string result = "Hello mate!"
The most easiest way is to use Regex:
Regex yourRegex = new Regex(#"&.*;");
string result = yourRegex.Replace("Hello &145126451; mate!", String.Empty);
Console.WriteLine(result);
Here is a fiddle with example.

Categories