C# Regular expression Example for Integrity [duplicate] - c#

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 1 year ago.
Can anyone tell me what this kind of Regular Expression is doing?
(\s*(\s*#\s*(?\d+)\s*))
If I test it an add whitespaces it says there is no match.
Can somebody give me an example? :)

That regular expression is not correct. Try removing the ? character.
(\s*(\s*#\s*(\d+)\s*))
And this expression matches any number that has an # in front of it. For example, in the next lines, it matches lines 1,2,5 and 6.
#995
#995
995
995
#995 9
#654 233
233
You can test it in the following link:
http://regexstorm.net/tester?p=%28%5cs*%28%5cs*%23%5cs*%28%5cd%2b%29%5cs*%29%29&i=++%23995%0d%0a%23995%0d%0a++995%0d%0a995%0d%0a%23995+9%0d%0a++%23654+233%0d%0a233

Related

c# Regex - Check if condition in a number of a certain length is met [duplicate]

This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 2 years ago.
A friend of mine had this as a task for university.
He needed a Regex to check if a 9 digit long number with leading 0+ has no more 0s after another digit.
Examples:
000001111 - good
000000010 - bad
000000000 - good
001215341 - good
165160000 - bad
0165168546 - bad
After some time we both came up with ^\b([0]+[1-9]*)\b$.
However this also allows the latest example/ does not check for the correct length. He ended up checking the length with an if-statement beforehand, but I just can't find my peace because of it.
Is there an more elegant way? One Regex to do the above, but also check the length?
You may use this regex with a lookahead condition:
^(?=\d{9}$)0*[1-9]*$
RegEx Demo
^: Start
(?=\d{9}$): Lookahead condition to check for exact 9 digits
0*: Match 0 or more 0
[1-9]*: Match 0 or more [1-9] digits
$: End

How to create Regular expression from give conditions? [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Learning Regular Expressions [closed]
(1 answer)
Closed 2 years ago.
Actually i am just beginer to learn regex but now i have a problem.
Can somebody help me to create regex for the follwing condtions:
Text lenght should be 6
1st character should be in Caps
2nd character should be in lower
3rd character should be numeric
4th character should be in lower again
5th character should be in Caps again
6th character should be in numeric again
But shouldn't contain any special character
You can do it pretty easily.
([A-Z][a-z]\d{1}[a-z][A-Z]\d{1})

Start of string to have one X and not 2 X's [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I am trying to find a way of checking if a string starts with only one "X" but not 2 X's in a regular expression.
So I think I have tried
Regex.IsMatch(teststr, "^[^X]{1}?^[XX]");
Regex.IsMatch(teststr, "^[^X]{1}?");
Regex.IsMatch(teststr, "^[^X]{1}");
Regex.IsMatch(teststr, "^[^X]?");
The results should be, if string Starts with 1 X only then give me all except the ones that start with only 1 X. A string with 2 X's is allowed
What about using a negative lookahead?:
^X(?!X)
Basically the (?!pattern) part denotes a negative lookahead, which will fail your overall expression matching if the pattern in the lookahead fails.
Try it online

RegEx to match only {n} digits and not much or less surrounded by whatever [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I'm trying to match only {n} digits not more or less and may be surrounded by characters or special symbols
example:
suppose the {n} = {14}
*12345678901234*300 OK
12345678901234x21 OK
*123456789012345*300 NOT OK
12345678901234 OK
123456789012345 NOT OK
You could use negative lookarounds to assert what is directly on the left and right is not a digit and match 14 digits:
(?<!\d)\d{14}(?!\d)
.NET regex demo
(?:^|\D)(\d{14})(?:\D|$)
Here is a Live Demo

Regular expression for my password policy [duplicate]

This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
My password strength criteria is as below :
Character length: 8 -> 24
Contains least 3 of following types:
Uppercase letter [A-Z]
Lowercase letter [a-z]
Numeric [0-9]
Special character ~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/
Can anyone help me to make regular expression and explain. Thanks.
It would be very difficult to do in one regex. Personally, I'd check for each case separately and count if you've got three matches. It would be easier to read and maintain.
So match [A-Z] then [a-z] then [0-9] and finally [~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/]
If you end up with a match in three of the tests that's success.
Use this Regex:
(?=.{8,24})(?=.*\d)(?=.*[A-Za-z])(?=.*[~`!##\$%\^&\*()_=\+[{\]}\|;:'"<,>\.\?/]).*
Debuggex Demo

Categories