RegEX : Find match based on condition [closed] - c#

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

Related

Regex to remove c# [closed]

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/.

How to find __tokens__ in a string without using RegEx in C# [closed]

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.

Split calculator expression with binary and unary operations [closed]

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(" ");

Regex to get last 4 characters before first occurrence of "/" [closed]

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)

Regular Expression matching empty string before a character in c# [closed]

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 am trying to match a string containing nothing before a character.
Consider the following lines:
'this is a valid line
'this is also a valid line
Any string ' this is an invalid line
I Need a regular expression that matches the first two line and does not match third line.
Basic regex tried was '.* but it matches all the three lines:
So need a regex that does not match a non empty string before '
perhaps try anchor at the beginning... ^\s*'.*

Categories