Regular Expression for a code that starts with specific letters [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 6 years ago.
Improve this question
How can I build a regular expression that will check this code:
ABC00000
The ABC is fixed and the 0 is a place holder for numbers.
The maximum length of numbers is 5.

Use this Regex:
ABC(\d{5})
The capturing group will also help you to retrieve the number after 'ABC', if you need it.
A non-regex way:
testString = "ABC00000";
if (testString.StartsWith("ABC") && int.TryParse(testString.Substring(3), 0))
{
}
The above code basically checks if the first three characters are 'ABC' and the last 5 characters are numeric. The int.TryParse() function returns if the number is parse-able from the string, i.e., if it is a number.

Related

How to validate textbox to enter only numerical or one string value that is 'AB' [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
How to validate textbox to enter only numerical or one string value that is 'AB'
means the in that textbox you can enter either numerical or 'AB'
so.. how to do
Other than the commented suggestion for RegEx, you can use TryParse() method like
int number;
if(Int32.TryParse(txt1.Text, out number) || txt1.Text == "AB")
//Your code here

Why can't I parse "0-2-6-1​" to ints after splitting by '-' [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 6 years ago.
Improve this question
Reading from an Excel Worksheet that was copy and pasted with a list of string speperated numbers, why will c# not convert a char/string 0 to a zero or a char/string 1 to an int 1 after splitting the string into an array by '-'?
It seems like there is a character problem, the difference between a 1 and a 1 with no line at the bottom?
Thanks
The result of spliting a string is an array of string, which in case they are numeric you should further parse into integer.
var str = "0-2-6-1";
var stringValues = str.Split('-'); // string[] values
var intValues = Array.ConvertAll(stringValues, s => int.Parse(s)); // int[] values

how do i display how many digits the number has? [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
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.

What does string.IndexOf do? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm working through string manipulation, where I need to find characters or copy some part of the string that the user has input and divide it to 3 different areas. I'm not asking anything here about code, I'll do it myself, but I was searching in the documentation that Microsoft provide about the IndexOf method and its 8 overloads and I can't really understand how to apply it. I simply can't understand what it is supposed to do.
Returns the first appearance of a specified char.
For example
string x = "Hello World";
x.indexOf("W");
it will return 6 (0 based count).
The overloads let you choose for example, where you want to start searching.. like
x.indexOf("W", 7); it will return -1 because W is at position 6 so if starts at 7 it won't find any.
I hope this helps ! the best way is to play with it
This also works with arrays.
I believe the MSDN explanation is pretty clear.
For example:
string something = "something";
int indexOfT = something.IndexOf("t"); // => returns 4
Reports the zero-based index of the first occurrence of the specified string in this instance.
So if "t" is in the fifth position of "something", 4 would be it's zero-based index.

C# Regex that captures a percentage in a named group [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 have the following regex pattern:
-?\d+(?:\.\d+)?%
Which works really well to match percentages. Now what I need is to be able to get the numerical value of this in a group.
I have tried the following:
(?<percentage>-?\d+(?:\.\d+)?)%
(?<percentage>-?\d+(\.\d+)?)%
(?<percentage>(-?\d+(?:\.\d+)?))%
(?<percentage>(-?\d+(\.\d+)?))%
None of them work and I only get the integer part of the percentile.
How can this be accomplished?
The correct regex is:
(?<percentage>-?(?:\d*\.?\d+|\d+\.?\d*))%
I tried the following examples and they all seemed to work (using the first regex that you tried):
Regex.Match("12%", #"(?<percentage>-?\d+(?:\.\d+)?)%").Groups["percentage"].Value
// 12
Regex.Match("12.345%", #"(?<percentage>-?\d+(?:\.\d+)?)%").Groups["percentage"].Value
// 12.345
Regex.Match("-12.345%", #"(?<percentage>-?\d+(?:\.\d+)?)%").Groups["percentage"].Value
// -12.345
You might note the use of the "#" in front of the regular expression string - maybe that is the issue you are having.

Categories

Resources