Well my question is simple, I want to match a string with following attributes
No white space
Must start with a letter
Must not contain any other special characters other than underscore
May contain numbers
Please help in creating such a regex.
^[a-zA-Z][a-zA-Z0-9_]*$
Dissecting it:
^ start of line/string
[a-zA-Z] starts with a letter
[a-zA-Z0-9_]* followed by zero or more letters, underscores or digits.
$ end of line/string
If you need to consider Unicode, then the following is probably more sane:
^\p{L}[\p{L}\p{Nd}_]*$
This will match not only ASCII letters and digits but across all scripts that are supported by Unicode. Digits are restricted to decimal digits, only, so you won't get Roman numerals.
/^[a-zA-Z]\w*$/
a-Z - start with letter
\w - all leters, numbers and underscore
Related
I need a regular expression for a string with has at least 8 symbols and only one uppercase character. Java
For example, it should match:
Asddffgf
asdAsadasd
asdasdaA
But not:
adadAasdasAsad
AsdaAadssadad
asdasdAsadasdA
I tried this: ^[a-z]*[A-Z][a-z]*$ This works good, but I need at least 8 symbols.
Then I tried this: (^[a-z]*[A-Z][a-z]*$){8,} But it doesn't work
^(?=[^A-Z]*[A-Z][^A-Z]*$).{8,}$
https://regex101.com/r/zTrbyX/6
Explanation:
^ - Anchor to the beginning of the string, so that the following lookahead restriction doesn't skip anything.
(?= ) - Positive lookahead; assert that the beginning of the string is followed by the contained pattern.
[^A-Z]*[A-Z][^A-Z]*$ - A sequence of any number of characters that are not capital letters, then a single capital letter, then more non capital letters until the end of the string. This insures that there will be one and only one capital letter throughout the string.
.{8,} - Any non-newline character eight or more times.
$ - Anchor at the end of the string (possibly unnecessary depending on your requirements).
In your first regex ^[a-z]*[A-Z][a-z]*$ you could append a positive lookahead (?=[a-zA-Z]{8,}) right after the ^.
That will assert that what follows matches at least 8 times a lower or uppercase character.
^(?=[a-zA-Z]{8,})[a-z]*[A-Z][a-z]*$
Some users a writing their messages in uppercase only, and I want to avoid that with JQuery Validation Engine.
I have tried many many regex without any success.
Here is the idea for a custom rule to avoid more than 10 uppercase characters:
uppercase: {
regex: /^(![A-Z]{10})+$/,
alertText: "* uppercase test alert"
},
I can't figure out what's wrong.
If you want to only allow strings with 10 and fewer uppercase letters, you may use
/^(?!(?:[^A-Z]*[A-Z]){11})/
See the regex demo
The pattern matches any string that does not contain 11 or more ASCII uppercase letters (so, it may contain 0 to 10 ASCII uppercase letters).
Details
^ - start of string
(?!(?:[^A-Z]*[A-Z]){11}) - a negative lookahead that fails the match if, immediately to the right of the current position, there are
(?:[^A-Z]*[A-Z]){11} - 11 occurrences of
[^A-Z]* - any 0+ chars other than uppercase ASCII letters
[A-Z] - an uppercase ASCII letter.
If you want to match a string that has no 10 uppercase ASCII letters on end:
/^(?!.*[A-Z]{11})/
See the regex demo.
Details
^ - start of the string
(?!.*[A-Z]{11}) - a negative lookahead that fails the math if there are 11 uppercase ASCII letters after any 0+ chars other than line break chars immediately the right of the current location.
Can someone please provide me with a regex pattern to match these requirements?
between 3 and 20 characters
begins with a letter
cannot end in a period(.)
can contain: a-z, 0-9, period(.), hyphen(-), underscore(_)
I'm new to regex. I've tried ^[[:alpha:]][[:alnum:]_.-]+$, given to me by someone, and I started to do my own with [a-z,A-Z,.]{3,20}[0-9]*
I will be using this in JavaScript but so far I've just been testing at regexr.com because it is convenient.
You can use /^[a-z][\w.-]{1,18}[\w-]$/i as a pattern. A little breakdown:
^ is an anchor for the start of the string, as we want to check the whole string
[a-z] is a character class matching letters a-z, lowercase and also uppercase due to the i-Modifier. This is used for your begins with a letter condition
[\w.-]{1,18} is a character class matching letters, numbers, underscore (= \w), dot and hyphen. It is repeated one to eighteen times to fit between 3 and 20 characters (+ 2 characters at start and end)
[\w-] is basically the same character class, but without the dot, to fit cannot end in a period(.)
$ is an anchor for the end of the string
I'm trying to match a string that contains alphanumeric, hyphen, underscore and space.
Hyphen, underscore, space and numbers are optional, but the first and last characters must be letters.
For example, these should all match:
abc
abc def
abc123
ab_cd
ab-cd
I tried this:
^[a-zA-Z0-9-_ ]+$
but it matches with space, underscore or hyphen at the start/end, but it should only allow in between.
Use a simple character class wrapped with letter chars:
^[a-zA-Z]([\w -]*[a-zA-Z])?$
This matches input that starts and ends with a letter, including just a single letter.
There is a bug in your regex: You have the hyphen in the middle of your characters, which makes it a character range. ie [9-_] means "every char between 9 and _ inclusive.
If you want a literal dash in a character class, put it first or last or escape it.
Also, prefer the use of \w "word character", which is all letters and numbers and the underscore in preference to [a-zA-Z0-9_] - it's easier to type and read.
Check this working in fiddle http://refiddle.com/refiddles/56a07cec75622d3ff7c10000
This will fix the issue
^[a-zA-Z]+[a-zA-Z0-9-_ ]*[a-zA-Z0-9]$
I tried using following regex:
/^\w+([\s-_]\w+)*$/
This allows alphanumeric, underscore, space and dash.
More details
As per your requirement of including space, hyphen, underscore and alphanumeric characters you can use \w shorthand character set for [a-zA-Z0-9_]. Escape the hyphen using \- as it usually used for character range inside character set.
To negate the space and hyphen at the beginning and end I have used [^\s\-].
So complete regex becomes [^\s\-][\w \-]+[^\s\-]
Here is the working demo.
You can use this regex:
^[a-zA-Z0-9]+(?:[\w -]*[a-zA-Z0-9]+)*$
RegEx Demo
This will only allow alphanumerics at start and end.
I need help using regular expression. I have some strings with these possibility use cases. The string will always start with a capital letters followed by 3 numbers then with a hyphen, followed by numbers:
A012-123
B001-012
C023-456
I've tried: [A-Z0-9]-[0-9] and i can't get a match. Can someone show me how to construct this correctly?
[A-Z][0-9]{3}-[0-9]{3}
The {3} means that, match only three times. This will match any string which starts with a capital letter, followed by 3 digits, a hypen and 3 digits.
But if the number of digits after - can be anything, then you can use
[A-Z][0-9]{3}-[0-9]+
This match any string which starts with a capital letter, followed by 3 digits, a hypen and one or more digits.
Note: Instead of writing [0-9], you can use \d. They both are one and the same. So your first regex will become
[A-Z]\d{3}-\d{3}