Checking the pattern containing numeric values, comma, space and colon in Regex - c#

I have a string in c#, say str. Using regex, how can I check if it matches the format 'n, To: n'.
Here n is a numeric value between 0 and 4999.

If the numbers need not be same:
([0-4]?\d{1,3}) To:([0-4]?\d{1,3})
If the numbers need to be the same; your pattern will be:
([0-4]?\d{1,3}) To:\1

Related

Regex Spilt based on multiple delimiters in C#

I have a string of type "KeyOperatorValue1,Value2,Value2....". For e.g = "version>=5", "lang=en,fr,es" etc and currently, the possible value for operator field is "=", "!=", ">", ">=", "<", "<=", but I don't want it to be limited to them only. Now the problem is given such a string, how can I split into a triplet?
Since, all the operator's string representation are not mutually exclusive("=" is a subset of ">="), I can't use public string[] Split(string[] separator, StringSplitOptions options) and the Regex.Split doesn't have a variant which takes multiple regex as parameters.
Since you have not mentioned the format of your input I have made certain assumptions..
I have assumed that
key would always contains alphanumeric characters
values would always be alphanumeric characters optionally separated by ,
key-value pair would be separated by non word characters
(?<key>\w+)(?<operand>[^\w,]+)(?<value>[\w,]+)
So this would match a string as operand if its not , or any one of [a-zA-Z\d_]
You can use this code
var lst=Regex.Matches(input,regex)
.Cast<Match>()
.Select(x=>new{
key=x.Groups["key"].Value,
operand=x.Groups["operand"].Value,
value=x.Groups["value"].Value
});
You can now iterate over lst
foreach(var l in lst)
{
l.key;
l.operand;
l.value;
}
Regex has "or" operator (separators will be included in the result though):
Regex.Split(#sourceString, #"(>=)|(<=)|(!=)|(=)|(>)|(<)");
You don't have to use regular expressions to accomplish that. Simply store the operators in an array. Keep the array sorted by the length of the operators. Iterate over the operators and get the position of the operator using IndexOf(). Now you can use Substring() to extract the key and the values from your input string.
You can just use branching to provide multiple alternatives. There are multiple possibilities to achieve this, one example would be this:
(\w+)([!<>]?=|[<>])(.*)
As you can see this expression contains three separate capture groups:
(\w+?): This will match "word" character (alphanumerical and underscores), as long as the sequence is at least one character long (+).
([!<>]?=|[<>]): This expression matches the operators given in your example. The first half ([!<>]?=) will match any of the characters inside [] (or skip it (?)) followed by =. The alternative simply matches < or >.
(.*): This will match any character (or nothing), whatever follows till the end of the string/line.
So when you match the expression, you'll get a total of 4 (sub) matches:
1: The name of the key.
2: The operator used.
3: The actual value given.
Edit:
If you'd like to match other operators as well, you'd have to add them as additional branches in the second matching group:
(\w+)([!<>]?=|[<>]|HERE)(.*)
Just keep in mind that there's in general no 100% perfect way to match any operator without defining the exact characters that should be considered valid operands (or components of an operand).

regex not matching number correctly

I have the following regex: (\d{14}) decimal that matches 14 character long number. The problem is that it also matches numbers, that are 16 characters long. I need to add a condition to match if there are no numbers at beginning or end of string.
So for example 112222222222222233 wouldn't be a match i want, but xx22222222222222xx would be match I need.
use word boundary \b
\b\d{14}\b
M42's answer can work in cases where the number is delimited by spaces or other word delimiters. But if you want to match a number in a word containing non-digits (like your example xx22222222222222xx) something like this should work:
(^|[^\d])\d{14}([^\d]|$)

Using regular expressions to check for a minimum number of characters?

I have the following code to validate usernames for an application:
Regex usernameRegex = new Regex("[A-Za-z0-9_]");
if (usernameRegex.IsMatch(MyTextBox.Text)) {
// Create account, etc.
}
How would I modify my regular expression to check if the username has a certain number of characters?
This expression validates only all text which contains any combination of A to Z, a to z and number 0 to 9. You can define the length of the string using the regex:
Regex reg= new Regex(#"^[A-Z]{3,}[a-z]{2,}\d*$")
{3,} and {2,} mean here that the string must have at least 3 capital characters, at least 2 small characters, and any amount of digit characters.
For example :
Valid : AAAbb, AAAbb2, AAAAAAbbbbb, AAAAAbbbbbb4343434
Invalid: AAb, Abb, AbAbabA, 1AAAbb,
To set a minimum (or maximum) range in a regular expression you can use the {from,to} syntax.
The following will only match a string with a minimum of 5 alpha numeric and underscore characters:
[A-Za-z0-9_]{5,}
And the following will match a minimum of 5 and maximum of 10:
[A-Za-z0-9_]{5,10}
[A-Za-z0-9_]
[] "brackets": are a group of characters you want to match.
A-Z: means it will match any alphabet capitalized within this range A-Z.
a-z: means it will match any small alphabet within this range a-z.
0-9: means it will match any digit in this range 0-9.
_: means it will match the "_" character.
now this regex will usually match the following: any character from a to z (small, capital), any number (from 0-9) and the underscore "_".
i.e. "a.,.B.,10.._" this will match "a, B, 10, _". but of course you need to add the singleline regex option.

C# RegEx for decimal number or zero (0)

This gets all numbers but not floating numbers
Regex(#"^\d+$")
I need it to get these values as well:
1234.12345545
0
12313
12313.13
-12131
-1313.13211312
For matching all of the above; the most appropriate regex is probably
#"^[+-]?\d+(\.\d+)?$"
This matches all of the above; but not numbers on the format .3456.
It also matches numbers on the format +123 and -1234.5678
Try this here
^(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?$
This will additionally match the empty string.
See it online here on Regexr
If matching the empty string is not wanted, then you can add a length check to your regex like
^(?=.+)(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?$
The positive lookahead (?=.+) ensures that there is at least 1 character

c# Regex question: only letters, numbers and a dot (2 to 20 chars) allowed

i am wrestling with my regex.
I want to allow only letters and numbers and a dot in a username, and 2 to 20 chars long
I thought of something like this
[0-9a-zA-Z]{2,20}
but then 21 chars is also ok, and that's not what i want
I suggest that you make two checks -- one for length and one for content based on the fact that you probably only want one dot in the name, rather than any number of dots. I'll assume that names like username and user.name are the only formats allowed.
This should get the content( but allows underscores as well):
^\w+(\.\w+)?$
If you don't want underscores, then you would use [0-9a-zA-Z]+ in place of \w+. To explain, it will match any string that consists of one or more word characters, followed by exactly 0 or 1 of a dot followed by one or more word characters. It must also match the beginning and end of the string, i.e., no other characters are allowed in the string.
Then you only need to get the length with a simple length check.
^[0-9a-zA-Z\.]{2,20}$
Try ^[\w\.]{2,20}$ instead.
You need to use start and end of string (^ and $), and escape the .:
^[0-9a-zA-Z\.]{2,20}$

Categories