Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to remove some string from regex in c#.
I have a string
000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip
From this I want to remove those strings
140702
142110
And I want a result as
000603ABC-005051-I-FILL200-NNYYNY180319--A2002.zip
How can I do that in regex?
You can use the Regex.Replace method (https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx).
var regex = new Regex("(140702)|(142110)");
var result = regex.Replace("000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip", "");
Although, as already stated by #CompuChip, String.Replace would probably be more efficient in this case. There's a discussion of various methods of replacing substrings and their efficiency at https://blogs.msdn.microsoft.com/debuggingtoolbox/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance/.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following:
The __animal__ jumped over the __object__
I'd like to write a concise method in C# that returns animal and object. How can this be done without RegEx? A dirty solution would be to iterate through the characters one by one until we find a __ and then build a string until we find the closing __ - but I'm looking for a more elegant approach.
You are going to have to iterate in some form - if you don't want to use regex.
string text = "The __animal__ jumped over the __object__";
List<string> words = text.Split(' ').ToList();
words = words.Where(x => x.StartsWith("__") && x.EndsWith("__")).ToList();
You can use extensions of IEnumerable.to search, like above. but the framework is technically iterating.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I get string input like: 1+2*5+sin100-2 and In order to calculate it I need to make array that will look like:
1,+,2,*,5,+,sin,100,-,2
Is there easy solution to do this problem?
Currently I am doing it with loop on all the chars in the string but I want to know if c# offer easiest solution to this problem.
I found easy solution. You could just write it instead of complain that there is no code in the question.
string expression;//my calculator expression
string[] operators;//array that contains both unary and binary operators.
for(int i=0;i<operators.length;i++)
{
expression = expression.replace(operators[i]," "+operators[i]+ " ");
}
string[] Values= expression.split(" ");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Below are the test input . The result I want is AAA- and AB-- respectively. The input string has no specific pattern. So first occurrence of / and the 4 character before that is the result I want to have.
Test line abc (r);AAA-/2010/001
Test line abc (r);AB--/2010/001
Like that? (EDIT: in .NET regex patterns, / does not need escaping)
(.{4})/
Consider sites like https://regex101.com for future needs)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to RegEx and looking for a solution to this condition. I am writing my code in C# and don't want to use any STRING functions.
Below is a sample string that I get:
610WBDFGFGM0122544
Now the condition I need to check is,
If string starts with "6" && 6th character is "D"
Can anyone tell me how to write RegEx for the above condition?
You can use this regex:
^6.{4}D
Here, ^ is the start of the string, .{4} means any four characters.
Online demo
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Which is the best way I can compare two C# string variables containing comma separated values and find the difference?
The scenario is like.
string variable1 = "AAA, BBB, CCC, DDD";
string variable2 = "AAA, CCC, DDD, EEE";
And I need the result like "BBB" (the value which is present in variable2 but not in variable1.
Thanks
Use Except:-
var result = variable1.Split(new char[] {','})
.Except(variable2.Split(new char[] {','})).ToArray();