I have to make regex expression for e-mail.
It is allowed to have letters or numbers only before the # symbol, optionally only the dot that can not be on the start. After # symbol it is allowed to have letters or numbers only, exactly one dot and after dot at least 2 characters.
That's my expression
/([a-zA-Z\d*])+(\.?)([a-zA-Z\d*])*#{1}([a-zA-z\d*])+(\.){1}([a-zA-Z\d*]){2,}/
Whole email should have between 10 and 30 characters, and I don't know how to do that.
Thanks
Actually, you can prefix your pattern with a look-ahead and a quantifier to match between 10 and 30 characters: (?=^.{10,30}$)
Then, your pattern looks like this:
(?=^.{10,30}$)([a-zA-Z\d*])+(\.?)([a-zA-Z\d*])*#{1}([a-zA-z\d*])+(\.){1}([a-zA-Z\d*]){2,}
Demo
The syntax for a range of allowed repeats is {n,m}. You wrote {1} meaning "exactly one" which is pointless. {10,30} is the range you are looking for.
Also, know the escape code for "letters". \w is a "word character", which is the same as [a-zA-Z0-9_]. And why is there a '*' in the character range?
So the problem is that you have pieces which can end up being of various lengths, and need to check the total when done, right?
In Perl you can include code as a assertion. So include (?{ length($&) <= 30 }) as the final assertion.
^(?=^.{10,30}$)(?=^[A-Za-z0-9])[A-Za-z0-9\.]+#[A-Za-z0-9]+\.[A-Za-z]{2,}$
https://regex101.com/r/cBACF2/10
Should be between 10 to 30 (?=^.{10,30}$)
Start with letters & numbers (?=^[A-Za-z0-9])
contains letters & numbers and dot [A-Za-z0-9\.]
at least two letter after after last dot [A-Za-z]{2,}
Related
I want to match 5 to 20 character with regex.
I try to use below regular expression for my checking.
/^[a-zA-Z][\w]{5,20}$/
It's work, but the problem of length it match 6 to 21 character.
(^[a-zA-Z][\w]){4,20}$
I also try this but it don't work.
Please anyone help me to match exact length of regex.
It's because your capturing group is expecting TWO characters:
[a-zA-Z] and [\w], that's two letters.
So your first attempt actually did this:
match [a-zA-Z] once
match [\w] once
match the previous matches 5 - 20 times
Inevitably, you always had 1 more match than expected
Capture only one character, and iterate it 5-20 times.
Have you tried:
^([a-zA-Z]{5,20})$ ?
OR
^(\w{5,20})$ ?
You're almost there, you just need to make a single range of characters (in square brackets) not two.
/^[a-zA-Z][\w]{5,20}$/ means:
a character from a to z in lower or upper case
5 to 20 word characters
That sums up to 6 to 21 characters in total.
I suppose you want /^[a-zA-Z][\w]{4,19}$/:
a character from a to z in lower or upper case
4 to 19 word characters
That sums up to 5 to 20 characters in total.
The Quantifier is only applied to the [\w]. So this expects exactly one letter character and then 5-20 whitespace characters.
I assume you want 5-20 characters that can be either a letter a-z or a whitespace. You need to group these together in square brackets and then apply the quantifier:
^[a-zA-Z\W]{5,20}$
So, I understand, you want a string that has 5-20 characters, starts with a letter and then only has letters and digits. You would write it like that:
^[a-zA-Z][a-zA-Z0-9]{4,19}$
This expects first a letter and then 4-19 letters or digits.
BTW: https://regex101.com/ is a great site to test regular expressions and get an explanation what they are doing.
I am trying to write a regex to max a sequence of numbers that is 5 digits long or over, but I ignore any spaces, dashes, parens, or hashes when doing that analysis. Here's what I have so far.
(\d|\(|\)|\s|#|-){5,}
The problem with this is that this will match any sequence of 5 characters including those characters I want to ignore, so something like "#123 " would match. While I do want to ignore the # and space character, I still need the number itself to be 5 digits or more in order to qualify at a match.
To be clear, these would match:
1-2-3-4-5
123 45
2(134) 5
Bonus points if the matching begins and ends with a number rather than with one of those "special characters" I am excluding.
Any tips for doing this kind of matching?
If I understood requirements right you can use:
^\d(?:[()\s#-]*\d){4,}$
RegEx Demo
It always matches a digit at start. Then it is followed by 4 or more of a non-capturing group i.e. (?:[()\s#-]*\d) which means 0 or more of any listed special character followed by a digit.
So just repeat a digit, followed by any other sequence of allowed characters 5 or more times:
^(\d[()\s#-]*){5,}$
You can ensure it ends on a digit if you subtract one of the repetitions and add an explicit digit at the end:
^(\d[()\s#-]*){4,}\d$
You can suggest non-digits with \D so et would be something like:
(\d\D*){5,}
Here is a guide.
New to Regular Expressions. Thanks in advance!
Need to validate field is 1-10 mixed-case alphanumeric and spaces are allowed. First character must be alphanumeric, not space.
Good Examples:
"Larry King"
"L King1"
"1larryking"
"L"
Bad Example:
" LarryKing"
This is what I have and it does work as long as the data is exactly 10 characters. The problem is that it does not allow less than 10 characters.
[0-9a-zA-Z][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ][0-9a-zA-Z ]
I've read and tried many different things but am just not getting it.
Thank you,
Justin
I don't know what environment you are using and what engine. So I assume PCRE (typically for PHP)
this small regex does exact what you want: ^(?i)(?!\s)[a-z\d ]{1,10}$
What's going on?!
the ^ marks the start of the string (delete it, if the expression must not match the whole string)
the (?i) tells the engine to be case insensitive, so there's no need to write all letter lower and upper case in the expression later
the (?!\s) ensures the following char won't be a white space (\s) (it's a so called negative lookahead)
the [a-z\d ]{1,10} matches any letter (a-z), any digit (\d) and spaces () in a row with min 1 and max 10 occurances ({1,10})
the $ at the end marks the end of the string (delete it, if the expression must not match the whole string)
Here's also a small visualization for better understanding.
Debuggex Demo
Try this: [0-9a-zA-Z][0-9a-zA-Z ]{0,9}
The {x,y} syntax means between x and y times inclusive. {x,} means at least x times.
You want something like this.
[a-zA-Z0-9][a-zA-Z0-9 ]{0,9}
This first part ensures that it is alphanumeric. The second part gets your alphanumeric with a space. the {0,9} allows from anywhere from 0 to 9 occurrences of the second part. This will give your 1-10
Try this: ^[(^\s)a-zA-Z0-9][a-z0-9A-Z ]*
Not a space and alphanumeric for the first character, and then zero or more alphanumeric characters. It won't cap at 10 characters but it will work for any set of 1-10 characters.
The below is probably most semantically correct:
(?=^[0-9a-zA-Z])(?=.*[0-9a-zA-Z]$)^[0-9a-zA-Z ]{1,10}$
It asserts that the first and last characters are alphanumeric and that the entire string is 1 to 10 characters in length (including spaces).
I assume that the space is not allowed at the end too.
^[a-zA-Z0-9](?:[a-zA-Z0-9 ]{0,8}[a-zA-Z0-9])?$
or with posix character classes:
^[[:alnum:]](?:[[:alnum:] ]{0,8}[[:alnum:]])?$
i think the simplest way is to go with \w[\s\w]{0,9}
Note that \w is for [A-Za-z0-9_] so replace it by [A-Za-z0-9] if you don't want _
Note that \s is for any white char so replace it by if you don't want the others
I need to find the text of all the one-digit number.
My code:
$string = 'text 4 78 text 558 my.name#gmail.com 5 text 78998 text';
$pattern = '/ [\d]{1} /';
(result: 4 and 5)
Everything works perfectly, just wanted to ask it is correct to use spaces?
Maybe there is some other way to distinguish one-digit number.
Thanks
First of all, [\d]{1} is equivalent to \d.
As for your question, it would be better to use a zero width assertion like a lookbehind/lookahead or word boundary (\b). Otherwise you will not match consecutive single digits because the leading space of the second digit will be matched as the trailing space of the first digit (and overlapping matches won't be found).
Here is how I would write this:
(?<!\S)\d(?!\S)
This means "match a digit only if there is not a non-whitespace character before it, and there is not a non-whitespace character after it".
I used the double negative like (?!\S) instead of (?=\s) so that you will also match single digits that are at the beginning or end of the string.
I prefer this over \b\d\b for your example because it looks like you really only want to match when the digit is surrounded by spaces, and \b\d\b would match the 4 and the 5 in a string like 192.168.4.5
To allow punctuation at the end, you could use the following:
(?<!\S)\d(?![^\s.,?!])
Add any additional punctuation characters that you want to allow after the digit to the character class (inside of the square brackets, but make sure it is after the ^).
Use word boundaries. Note that the range quantifier {1} (a single \d will only match one digit) and the character class [] is redundant because it only consists of one character.
\b\d\b
Search around word boundaries:
\b\d\b
As explained by the others, this will extract single digits meaning that some special characters might not be respected like "." in an ip address. To address that, see F.J and Mike Brant's answer(s).
It really depends on where the numbers can appear and whether you care if they are adjacent to other characters (like . at the end of a sentence). At the very least, I would use word boundaries so that you can get numbers at the beginning and end of the input string:
$pattern = '/\b\d\b/';
But you might consider punctuation at the end like:
$pattern = '/\b\d(\b|\.|\?|\!)/';
If one-digit numbers can be preceded or followed by characters other than digits (e.g., "a1 cat" or "Call agent 7, pronto!") use
(?<!\d)\d(?!\d)
Demo
The regular expression reads, match a digit (\d) that is neither preceded nor followed by digit, (?<!\d) being a negative lookbehind and (?!\d) being a negative lookahead.
I have a text field which I need to validate using a regex. My requirement is as follow:
CCCCNNNNNN or CCCCNNNNNNN (Template)
1234ABCDEFG or 123-ABCDEFG (Example string)
Rules:
The whole string is maximum 25 characters
The first four characters (CCCC) must be alphanumeric
CCCC is 4 characters exactly and can be digits or number
CCCC can have a dash sign as 4th character
NNNNNNNNNNNN can be up to 21 characters and only numbers
E.g. AAAA 1234 A58- is a valid string for CCCC.
Here is my research notes:
I will need to match numerics first
I will need the + char to specify to match this pattern X times
I will need to match letters after that for 8-9 spaces
There is a wonderful post on RegEx patterns here:
Matching numbers with regular expressions — only digits and commas
My goal is to apply this REGEX pattern to a text box Mask in a WinForms app.
....
....
...yeah - I think the answer you are looking for (and I stress "think") is this expression:
^[0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}$
thats:
^ # assert beginning (not in the middle)
[0-9A-Za-z]{3} # three characters that are: 0-9 or a-z (upper or lower)
[0-9A-Za-z-] # one character that is: 0-9 or a-z (upper or lower) or a dash
\d{0,21} # anywhere from 0 to 21 digits
$ # assert at the end (not somewhere in the middle
If you want to match several cases of this expression, put the above expression (minus the assertions) into parantheses (()) along with whatever is allowed to separate these values - I chose \s or "whitespace") and then use the + quantifier:
^([0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}\s+)+$
will match/validate the following input:
1234567890 AAAA123456789012345678901 GGG-123 hhh5 A1B2000000000
If you wanted something else, you'll have to ask a clearer question (there's a lot of contradiction and repetition in your question that makes it EXTREMELY confusing)