It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The input string: A+SUM(A)C+AB-C+SUM(A)+1
I want to replace A with 0, the result like this:0+SUM(A)C+AB-C+SUM(A)+1
or replace SUM(A) with 0, the result like this:A+SUM(A)C+AB-C+0+1
Thanks
Without Regex (because Regex is overkill for this):
var s = "A+SUM(A)+B-C";
var replaceBeginningA = s.Replace("A+", "0+");
var replaceSumA = s.Replace("SUM(A)", "0");
Console.WriteLine(replaceBeginningA); // 0+SUM(A)+B-C
Console.WriteLine(replaceSumA); // A+0+B-C
As pointed out in the comments though, you need to provide more detail if this input is expected to have a different format.
maybe:
replace ^A with 0
replace SUM\(A\) with 0
Try thus:
string replaced = Regex.Replace(input, #"\b(\w+)\+SUM\(\1\)", "0+0");
This will match anything of the form
foo+SUM(foo)
and replace it with 0+0
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
System.Web.HttpUtility.HtmlDecode not working for test %3cstrong%3ebold %3c/strong%3etest
Output should be <strong>bold </strong>test
I believe you want HttpUtility.UrlDecode in this case.
HtmlDecode is for things like <strong>
You are mixing up html encoding with url encoding. So it is normal that this does not work. Try using HttpUtility.UrlDecode()
Example for Url encoding:
%3cstrong%3ebold%3c/strong%3e
Example for HTML encoding:
<strong>bold</strong>
HttpUtility.HtmlDecode is used for converting HTML entities, e.g.
var sample = "<strong>bold </strong>test";
var result = HttpUtility.HtmlDecode(sample);
// result = "<strong>bold </strong>test"
You're looking for HttpUtility.UrlDecode, I believe, which surrenders:
var sample = "test %3cstrong%3ebold %3c/strong%3etest";
var result = HttpUtility.UrlDecode(sample);
// result = "<strong>bold </strong>test"
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How can I do the following in C# :
var re = /^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$/;
re.test('2013/03/05 15:22:00'); // returns true
You can use the Regex.IsMatch instead (docs).
Regex.IsMatch("2013/03/05 15:22:00", #"^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$"); // true if match
The below code should get you where you want to be.
Regex rx = new Regex(#"^\d{4}(\/\d{2}){2} \d{2}(:\d{2}){2}$");
String test = "2013/03/05 15:22:00";
if (rx.IsMatch(test))
{
//Test String matches
}
else
{
//Test String does not match
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to replace between two characters in a string based on their Unicode code point ?? Could any help please ??Many Thanks.
For example,
Replace (U0041 with U0066)
Use the \u escape code to write the characters:
str = str.Replace('\u0041', '\u0066');
Alternatively, convert the numbers into characters:
int char1 = 65;
int char2 = 102;
str = str.Replace((char)char1, (char)char2);
You can do it like this:
Console.WriteLine("ABC".Replace("\u0041", "\u0066"));
This produces the output fBC, because the unicode code point of u0041 (which is A) has been replaced with the code point of u0066 - an f.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of words and I want to remove them from an input string, could anyone tell me what is the better way to conduct this task?
This should work
string[] arrToCheck = new string[] { "try ", "yourself", "before " };
string input = "Did you try this yourself before asking";
foreach (string word in arrToCheck )
{
input = input.Replace(word, "");
}
MessageBox.Show("result is "+input);
input variable will now have a string which does not have those words in your array.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Continuing from my previous question, I now want to remove the number once I have found it and stored it in a variable.
Just a slight tweak to my response to your first question to instead use Regex.Replace method will git 'er done.
Don't worry I figured it out. Just need to find the length then delete the chars
(qual is the intergers found)
string length = qual.ToString();
int length2 = length.Length;
text.Remove(0, length2);
I think the following code resolves the root problem:
string originalString = "35|http://www.google.com|123";
string[] elements = originalString.Split(new char[] { '|' }, 2);
int theNumber = int.Parse(elements[0]); // 35
string theUrl = elements[1]; // http://www.google.com|123