alphanumeric with at least 1 character - c#

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}

Related

Regular expression that Must have at least one letter

I have a case where I am using a queue of regular expressions to filter out specific items in an Observer pattern. The filter will place the values in specific controls based on their values. However 1 of the controls pattern is that it can accept ANY ASCII Character. Let me list the filters in their order with the RegEx
Column Rule Regex
Receiving 7 DIGITS #"^[1-9]([0-9]{6}$)" --->Works
Count 2 digits, no leading 0 #"^[1-9]([0-9]{0,1})$" --->Works
Producer any ASCII char. #".*" --->too broad
MUST contain a letter
Is there a regular expression that will accept any set of ASCII characters, but 1 of them MUST be a letter (upper or lower case)?
#"^(?=.*[A-Za-z])$" -->Didn't work
examples that would need to go into expression
123 red
red
123 red123
red - 123
red
If you want to match the whole rang of ASCII chars you may use
#"^(?=[^A-Za-z]*[A-Za-z])[\x00-\x7F]*$"
If only printable chars are allowed use
#"^(?=[^A-Za-z]*[A-Za-z])[ -~]*$"
Note the (?=[^A-Za-z]*[A-Za-z]) positive lookahead is located right after ^, that is, it is only triggered at the start of a string. It requires an ASCII letter after any 0 or more chars other than an ASCII letter.
Your ^(?=.*[A-Za-z])$ pattern did not work because you wanted to match an empty string (^$) that contains (?=...) at least one ASCII letter ([A-Za-z]) after any 0+ chars other than newline (.*).
You could try [A-Za-z]+.
It matches when there is at least one letter. You want something more specific?
How about
^.*[a-zA-Z]+.*$ ?
Between start and end of line, accept any number of any characters, then at least one a-z/A-Z character, then again any number of any characters.

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 failing in max length

I want regex which will allow following format
1234567-8
123456B
Now here if user enter second pattern then he should be lock to enter maximum 7 characters so
1234568B
123456V1
this becomes invalid
I have tried
[0-9]{7}-[0-9]|[[0-9]{6}[A-z]{1}]{7,7}
but this fails
For the sample input you provided, you may use ^([0-9]{7}-[0-9]|[0-9]{6}[A-Za-z])$.
A bit more contracted version: ^[0-9]{6}(?:[0-9]-[0-9]|[A-Za-z])$.
Note that 1234567-8 has 7 digits and a hyphen followed with a digit, so the whole string length cannot be limited to just 7 characters all in all.
In .NET and almost all other regex flavors [A-z] is a mistake, as it can match more than just letters.
Placing a quantifier {1} into a character class makes it a simple symbol combination, so [{1}] matches either { or 1 or }.
The {7,7} (={7}) will not limit the whole string length to 7, as you do not have anchors (^ and $) around the expression and you "ruined" the preceding quantifiers by putting them into a character class.
I think this is what you need:
^(\d{7}-\d|\d{6}[A-Z])$
7 digits, dash, digit OR 6 digits, 1 large latin letter.
^\d{6}(?:\d-\d|[A-Z])$
It can satisfy well with 2 your above formats
1234567-8
123456B

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.

Categories

Resources