Convert pattern string to Interpolated string with dollar sign - c#

var pattern = getPattern(); //read from the config, something like "{x} abc {y}"
var x = $pattern; //not compiling
Is there a way to convert a pattern in string Interpolated string?

Related

Convert string which contains ascii mixed with text [duplicate]

I have a Unicode string from a text file such that. And I want to display the real character.
For example:
\u8ba1\u7b97\u673a\u2022\u7f51\u7edc\u2022\u6280\u672f\u7c7b
When read this string from text file, using StreamReader.ReadToLine(), it escape the \ to '\\' such as "\\u8ba1", which is not wanted.
It will display the Unicode string same as from text. Which I want is to display the real character.
How can change the "\\u8ba1" to "\u8ba1" in the result string.
Or should use another Reader to read the string?
If you have a string like
var input1 = "\u8ba1\u7b97\u673a\u2022\u7f51\u7edc\u2022\u6280\u672f\u7c7b";
// input1 == "计算机•网络•技术类"
you don't need to unescape anything. It's just the string literal that contains the escape sequences, not the string itself.
If you have a string like
var input2 = #"\u8ba1\u7b97\u673a\u2022\u7f51\u7edc\u2022\u6280\u672f\u7c7b";
you can unescape it using the following regex:
var result = Regex.Replace(
input2,
#"\\[Uu]([0-9A-Fa-f]{4})",
m => char.ToString(
(char)ushort.Parse(m.Groups[1].Value, NumberStyles.AllowHexSpecifier)));
// result == "计算机•网络•技术类"
This question came out in the first result when googling, but I thought there should be a simpler way... this is what I ended up using:
using System.Text.RegularExpressions;
//...
var str = "Ingl\\u00e9s";
var converted = Regex.Unescape(str);
Console.WriteLine($"{converted} {str != converted}"); // Inglés True

How to print string format which is entered from Console.Readline() aa1234aa, in this format aa-1234-aa?

Given an input string such as "KK1234KK" how can I have it print it out with additional formatting: "KK-1234-KK"? In code I had hoped to be able to do something like this:
string LicensePlate = "KK1234KK";
Console.WriteLine(string.Format("Custom: {0:##-####-##}", LicensePlate));
You can try using regex expression to get it in this format. Here I'm looking for the number and adding "-" on both sides.
string LicensePlate = "KK1234KK";
Regex regex = new Regex(#"\d+");
Match match = regex.Match(LicensePlate);
string output = Regex.Replace(LicensePlate, #"\d+","-"+match.Value+"-");

Same pattern of a special character in 2 strings. Check outline level code of two strings

I have two strings like A.B.C.D_E and in other string I have "..._*" as shown in str.
I want to check if both string are at the same outline level code and both have the same structure.
I tried the following.
string str= "*.*.*.*_*";
str= str.Replace("*", "");
So I will get only dots and dashes and I can count them.
string abc = "A.B.C.D_E";
var count = Regex.Matches(abc, ".").Count;
var countdash = Regex.Matches(abc, "_").Count;
count= count+countdash;
If count is equal to characters in string one than the string have same format.
Unfortunately this solution does not work for different formats.
Is there any better way of doing it?
what you can do is remove all characters except dot and dashes and compare them.
private string RemoveExtraText(string value)
{
var allowedChars = "._";
return new string(value.Where(c => allowedChars.Contains(c)).ToArray());
}
for your string "A.B.C.D_E". This function will return "...._". Compare it to the other string and you can find if both have same pattern
string abc = "*.*.*.*_*";
string str = "...._*";
bool isMatch = Regex.Replace(abc, "[^._]", "") == Regex.Replace(str,"[^._]","");
[^._] will match any characters that aren't a dot or dash

C# ASP.NET Reg Replace...Replace string with another string

I have this string here
string Thing1 = "12340-TTT";
string Thing2 = "®"
I am looking to use reg replace to replace the TTT with &reg.
I am told using reg replace it does not matter if its uppercase or lowercase.
How would I go about doing this?
string input = "12340-TTT";
string output = Regex.Replace(input, "TTT", "&reg");
// Write the output.
Console.WriteLine(input);
Console.WriteLine(output);
Console.ReadLine();
This should do the trick. You find "TTT" in a string and replace it with "&reg".
Try this:
string one = "1234-TTT";
string pattern = "TTT";
Regex reg = new Regex(pattern);
string two = "&reg";
string result = reg.Replace(one, two);
Console.WriteLine(result);
should give you the desired Result. And just for a good read if you should ever need some more complicated Regular Expressions: http://msdn.microsoft.com/en-us/library/vstudio/xwewhkd1.aspx
correct me if i'm wrong but i think it is same with that of replacing a string on a variable like this:
string Thing1 = "12340-TTT";
string Thing2 = Regex.Replace(Thing1 , "®", "anyString");
got it from here:
http://www.dotnetperls.com/regex-replace
cheers:)
For this, you can use the Regex.Replace method (documentation here)
There are several overloaded versions, but the most direct one for this is the Regex.Replace(String input, String regexPattern, String replacementString) version:
string Thing1 = "12340-TTT";
string Thing2 = "®";
string newString = System.Text.RegularExpressions.Regex.Replace(Thing1, "[Tt]{3}", Thing2);
If you are unfamiliar with regular expressions, the [Tt] define specific character group (any characters matching one of ones specified) and the {3} just indicates that it must be appear 3 times. So, this will do a case-insensitive search for a string of 3 T's (e.g., TTT, ttt, TtT, tTt, etc.)
For more on basic regex syntax, you can look here: http://www.regular-expressions.info/reference.html
Also, between the RegexOptions you can pass to the regex there is the IgnoreCase to make it case insensitive:
string Thing1 = "12340-TTT";
string Thing2 = "®"
var regex = new Regex("TTT", RegexOptions.IgnoreCase );
var newSentence = regex.Replace( Thing1 , Thing2 );

C# regular expression to find custom markers and take content

I have a string:
productDescription
In it are some custom tags such as:
[MM][/MM]
For example the string might read:
This product is [MM]1000[/MM] long
Using a regular expression how can I find those MM tags, take the content of them and replace everything with another string? So for example the output should be:
This product is 10 cm long
I think you'll need to pass a delegate to the regex for that.
Regex theRegex = new Regex(#"\[MM\](\d+)\[/MM\]");
text = theRegex.Replace(text, delegate(Match thisMatch)
{
int mmLength = Convert.ToInt32(thisMatch.Groups[1].Value);
int cmLength = mmLength / 10;
return cmLength.ToString() + "cm";
});
Using RegexDesigner.NET:
using System.Text.RegularExpressions;
// Regex Replace code for C#
void ReplaceRegex()
{
// Regex search and replace
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(#"\[MM\](?<value>.*)\[\/MM\]", options);
string input = #"[MM]1000[/MM]";
string replacement = #"10 cm";
string result = regex.Replace(input, replacement);
// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(result, "Replace");
}
Or if you want the orginal text back in the replacement:
Regex regex = new Regex(#"\[MM\](?<theText>.*)\[\/MM\]", options);
string replacement = #"${theText} cm";
A regex like this
\[(\w+)\](\d+)\[\/\w+\]
will find and collect the units (like MM) and the values (like 1000). That would at least allow you to use the pairs of parts intelligently to do the conversion. You could then put the replacement string together, and do a straightforward string replacement, because you know the exact string you're replacing.
I don't think you can do a simple RegEx.Replace, because you don't know the replacement string at the point you do the search.
Regex rex = new Regex(#"\[MM\]([0-9]+)\[\/MM\]");
string s = "This product is [MM]1000[/MM] long";
MatchCollection mc = rex.Matches(s);
Will match only integers.
mc[n].Groups[1].Value;
will then give the numeric part of nth match.

Categories