Regular expression with repetition? - c#

I need a regular expression that accepts only digits and dots, with these conditions:
between digits three must be only one dot '132.632.55'
digits can be repeat in between two dots '.112234563456789.'
the string starts with digits
digits with "." like this '123346547987.' can repeat many times
length of these digits is less than 50 characters
For example: 123456.258469.5467.15546

Given all the information in the question, I think this is the regular expression you need:
^(\d{1,50}\.)*\d{1,50}$
This will:
require that the string begins and ends with a digit
not require that there is a dot in there at all
ensure that each run of digits between dots is no longer than 50 digits
If you need it to have at least one dot, change the * to a +:
^(\d{1,50}\.)+\d{1,50}$

From what I can tell from your requirements, you want something like this:
^(\d{1,50}\.)*\d{1,50}$
That is, from one to 50 digits, optionally preceded by any number of groups of one to 50 digits, each group followed by a fullstop. I can't quite tell if you want something like 1233.456 to be invalid, since your requirement #2 implies that only digit groups between dots can contain repeat digits. In such a case, it'd be much simpler to perform the validation of individual digit groups after the fact.

Related

Regex match certain amount of character and allow space inbetween

I am currently working on a regex which needs to match exactly 8 digits. But sometimes it occurs that there are spaces or dots between those numbers. This is the regex that i am currently using.
([0-9\ ?.?]{7,16})
It works fine most of the time, but the problem I am having is that it sometimes matches number with a lot of spaces tailing it so you will get something like 1234/s/s/s/s (/s stands for space) Or sometimes it is only matching spaces.
What i want is a regex that always matches at least 8 digits and also allows spaces and dots without detecting less then 8 digits. I know it may be stupid question, but I couldn't find anything I need elswhere.
Your ([0-9\ ?.?]{7,16}) expression matches 7 to 16 occurrences of any character that is either a digit, or a space, or a ?, or .. Yes, the ? inside [...] is a literal ?, not a quantifier.
You need to use an expression that will match a digit ([0-9]) and then exactly 7 sequences of a space or period ([ .]) followed with 1 digit, and to make sure you are not matching the digits in 123.156.78.146 you may use special boundaries:
(?<!\d[ .]?)\d(?:[. ]?\d){7}(?![ .]?\d)
if the space or . can only be 0 to 1 in between digits; or - if the space/dot can appear 0 or more times,
(?<!\d[ .]*)\d(?:[. ]*\d){7}(?![ .]*\d)
See the regex demo
The (?<!\d[ .]*) is a negative lookbehind that will fail any match if it starts with a digit that is followed with .(s) or space(s), and the (?![ .]*\d) negative lookahead will fail the match if the 7 digits you need are followed with .(s) or space(s) and a digit.
To solve this, describe the problem to yourself. You want to match one digit followed by seven repetitions of space-or-dot followed by a digit. This leads to a regular expression like \d([ .]?\d){7}. To avoid collecting the seven captures add a :? after the (. To capture the whole string, enclose it in brackets. Adding both changes gives the expression (\d(:?[ .]?\d){7}). If more than one space or dot is allowed between the digits then change the ? to a *.
To get just the eight digits out of the string I suggest using the string captured above and replacing any spaces or dots with nothing.

Regex of phone number - how to make sure the user won't put +-*+-*+-*+-*?

I want to allow the user to enter only patterns like:
+9720545455454
056565656345
03-43434344
0546-4234234
*9090
+97203-0656534
Meaning, I don't want to allow the user to gibberish everything together, like:
+954-4343+3232*4343+-
+-4343-+5454+9323+234
How can I fixed this pattern
public static bool IsPhoneNumberCorrect(string phoneNumber)
{
return Regex.IsMatch(phoneNumber, #"^[0-9*+-]+$");
}
for that purpose?
If you do not care about digit group length, you can allow + or * only at the beginning, and then match initial digits and then optional groups of hyphen+digits:
return Regex.IsMatch(phoneNumber, #"^[+*]?\d+(?:-\d+)*$");
See demo
Note you can limit the number of hyphen+digit with a quantifier. Say, there can be none or 1:
^[+*]?\d+(?:-\d+)?$"
^
See another demo
And in case there can be more than 1, use a limiting quantifier:
^[+*]?\d+(?:-\d+){0,3}$"
^^^^^
Here, {0,3} means 0, 1, 2 or 3 repetitions of the hyphen+digits group.
^(?!(.*\+){2})(?!(.*-){2})(?!(.*\*){2})[0-9*+-]+$
YOu can use lookaheads to make sure special characters appear only once.See demo.
https://regex101.com/r/vV1wW6/1
What you first need to do is identify what exactly the pattern is. This doesn't need to be in code. In you example, I see a leading character, followed by a first number group, followed by an optional dash and second number group. The leading character can be +, * or 0. The number groups are one digit between 1 and 9 followed by one or more digits 0 to 9. Translating each element gives:
Leader: [+*0]
Dash: -
Number group: [1-9][0-9]+
Throwing everything together you get
[\+\*0][1-9][0-9]+(-[1-9][0-9]+)?
Some groups may have minimum and maximum lengths, you can still work that in changing + to {min, max}.

regular expression not working: repeated strings of digits

I was trying to create a regular expression to find repeated strings of digits.
eg:
1 -not matching
11 -matching
122 -matching
1234 -not matching
what i used is \d+. Tutorial are telling
the "+" is similar to "*", except it requires at least one repetition.
But when i tried it is matching with any number. Any idea why?
Update
The tutorial i tried : http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial
The repetition constructs in Regular Expressions, +, *, {x}, do not repeat "what you found the first time around", they repeat "the pattern that finds things".
So this:
\d+
Will not find one digit, then match a sequence of that digit, instead it will first find one digit, then try to find another digit, then another, etc.
If you want it to repeat "what it found" you have to explicitly say so:
(\d)\1+
The \1 here says "I will match whatever is in the first group again", this regular expression should match sequences of the same digit, instead of sequences of digits.
^\d*(\d)\1+\d*$
You can use this.See demo.\d+ would match any intergers 1 or more time.You need to use \1 to find repeated digits.
https://regex101.com/r/hI0qP0/4
It works properly. \d+ is not a repetition of a specific digit, it is a repetition of one or more \d. \d+ will match 1 (one or more digit), 12 (one or more digit), 122 (one or more digit)... you see the idea. If you want to see two or more repetitions, you'd need to say \d\d+ or \d{2,} - but this, too, says that you want two or more digits, not two or more of a same digit. To say that, you need backreferences: (\d)\1+ is two or more of a same digit: a digit we remember, then one or more of that remembered thing.

Regular Expression for password

I'm not really good at regular expressions. I need to do the following to validate if a password, entered by the user, is correct or not.
Criteria:
Must contain at least one number
Must contain at least one letter from A-Z or a-z (case does not matter as long as they enter is a letter).
The password must be a minimum of 8 characters
(?=.*\d)(?=.*[A-Za-z]).{8,}
The first part ((?=.*\d)) searches for at least one number, the second part ((?=.*[A-Za-z])) searches for at least one letter, and the last part (.{8,}) ensures it's at least 8 characters long.
You might want to put an upper limit on the length of the password like this:
^(?=.*\d)(?=.*[A-Za-z]).{8,30}$
The 30 in that spot limits it to 30 characters in length, and the ^ and $ anchor it to the start and end of the string.

alphanumeric with at least 1 character

Ok so I finally figured out that I need the following:
So the regular expression has to perform:
alphanumeric
at least 1 letter
must have between 4 and 8 characters (either a letter/digit).
i.e. an alphanumeric string, that must have at least 1 letter, and be between 4-8 in length. (so it can't be just all numbers or just all letters).
can all of this be done in a single regex?
I'm assuming you mean alphaunumeric, at least one letter, and 4 to 8 characters long.
Try this:
(?=.*[a-zA-Z])[a-zA-Z0-9]{4,8}
(?= - we're using a lookahead, so we can check for something without affecting the rest of the match
.*[a-zA-Z] - match for anything followed by a letter, i.e. check we have at least one letter
[a-zA-Z0-9]{4,8} - This will match a letter or a number 4 to 8 times.
However, you say the intention is for "it can't be just all numbers or just all letters", but requirements 1, 2 and 3 don't accomplish this since it's can be all letters and meet all three requirements. It's possible you want this, with an extra lookahead to confirm there's at least one digit:
(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{4,8}
The use of a-zA-Z isn't very international friendly, so you may be better off using an escape code for "letter" if available in your flavour of Regular Expressions.
Also, I hope this isn't matching for acceptable passwords, as 4 characters probably isn't long enough.
number 2 and 3 seem to contradict. The following will match alphanumeric between 4 and 8:
/[0-9a-zA-Z]{4,8}/
?Regex.IsMatch("sdf", "(?=.+[a-zA-Z])[a-zA-Z0-9]{4,8}")
false
?Regex.IsMatch("sdfd", "(?=.+[a-zA-Z])[a-zA-Z0-9]{4,8}")
true
?Regex.IsMatch("1234", "(?=.+[a-zA-Z])[a-zA-Z0-9]{4,8}")
false
Warning on **.* and .+:
// At least one letter does not match with .*
?Regex.IsMatch("1111", "(?=.*[a-zA-Z])[a-zA-Z0-9]{4,8}")
false
?Regex.IsMatch("1aaa", "(?=.+[a-zA-Z])[a-zA-Z0-9]{4,8}")
true
[a-zA-Z0-9]{4,8}
The first part specifies alphanumeric, and the 2nd part specifies from 4 to 8 characters.
Try: [a-zA-Z0-9]{4,8}

Categories

Resources