I need a regular expression that has the following constraints:
Contains only [A-Z0-9.*] characters
Is 1 to 15 characters in length
Must contain a * but only once.
Therefore, the following assertions:
ABC.123 invalid (no stars)
ABC.123* valid (star at end)
*ABC.123 valid (star at beginning)
ABC.*123 valid (star in the middle)
*ABC.123* invalid (more than one star)
My goal was to only have a single expression. I could obviously have an expression that asserts the first two constraints: [A-Z0-9.*]{1,15} and then a second expression to assert the third constraint \*{1}.
Is it possible to have an expression that essentially validates the overall structure of a string input, but then rewinds and re-evaluates the string with another expression?
Using a lookahead you can use this regex:
^(?=.{1,15}$)[A-Z0-9.]*\*[A-Z0-9.]*$
RegEx Demo
RegEx Breakup:
^: Start
(?=.{1,15}$): Positive lookahead to assert that we have 1 to 15 characters in input
[A-Z0-9.]*: Match zero or more [A-Z0-9.] characters
\*: Match an asterisk literally
[A-Z0-9.]*: Match zero or more [A-Z0-9.] characters
$: End
Related
I have a regular expression for allowing unicode chars in names(Spanish, Japanese etc), but I don't want to allow '.'(dot) anywhere in the string.
I have tried this regex but it fails when string length is less than 3. I am using xRegExp.
^[^.][\\pL ,.'-‘’][^.]+$
For Example:
NOËL // true
Sanket ketkar // true
.sank // false
san. ket // false
NOËL.some // false
Basically it should return false when name has '.' in it.
Your pattern ^[^.][\\pL ,.'-‘’][^.]+$ matches at least 3 characters because you use 3 characters classes, where the first 2 expect to match at least 1 character and the last one matches 1 or more times.
You could remove the dot from your character class and repeat that character class only to match 1+ times any of the listed to also match when there are less than 3 characters.
^[\p{L} ,'‘’-]+$
Regex demo
Or you could use a negated character class:
^[^.\r\n]+$
^ Start of string
[^.\r\n]+ Negated character class, match any char except a dot or newline
$ End of string
Regex demo
You could try:
^[\p{L},\-\s‘’]+(?!\.)$
As seen here: https://regex101.com/r/ireqbW/5
Explanation -
The first part of the regex [\p{L},\-\s‘’]+ matches any unicode letter, hyphen or space (given by \s)
(?!\.) is a Negative LookAhead in regex, which basically tells the regex that for each match, it should not be followed by a .
^[^.]+$
It will match any non-empty string that does not contain a dot between the start and the end of the string.
If there is a dot somewhere between start to end (i.e. anywhere) it will fail.
Using regular expression, I want to select only the words which:
are alphanumeric
do not contain only numbers
do not contain only alphabets
have unique numbers(1 or more)
I am not really good with the regex but so far, I have tried [^\d\s]*(\d+)(?!.*\1) which takes me nowhere close to the desired output :(
Here are the input strings:
I would like abc123 to match but not 123.
ab12s should also match
Only number-words like 1234 should not match
Words containing same numbers like ab22s should not match
234 should not match
hel1lo2haha3hoho4
hel1lo2haha3hoho3
Expected Matches:
abc123
ab12s
hel1lo2haha3hoho4
You can use
\b(?=\d*[a-z])(?=[a-z]*\d)(?:[a-z]|(\d)(?!\w*\1))+\b
https://regex101.com/r/TimjdW/3
Anchor the start and end of the pattern at word boundaries with \b, then:
(?=\d*[a-z]) - Lookahead for an alphabetical character somewhere in the word
(?=[a-z]*\d) - Lookahead for a digit somewhere in the word
(?:[a-z]|(\d)(?!\w*\1))+ Repeatedly match either:
[a-z] - Any alphabetical character, or
(\d)(?!\w*\1) - A digit which does not occur again in the same word
Here is a bit shorter & faster regex to make it happen since it doesn't assert negative lookahead for each character:
/\b(?=[a-z]*\d)(?=\d*[a-z])(?!\w*(\d)\w*\1)[a-z\d]+\b/ig
RegEx Demo
RegEx Details:
\b: Word boundary
(?=[a-z]*\d): Make sure we have at least a digit
(?=\d*[a-z]): Make sure we have at least a letter
(?!\w*(\d)\w*\1): Make sure digits are not repeated anywhere in the word
[a-z\d]+: Match 1+ alphanumericals
\b: Word boundary
You could assert all the conditions using one negative lookahead:
\b(?![a-z]+\b|\d+\b|\w*(\d)\w*\1)[a-z\d]+\b
See live demo here
The important parts are starting match from \b and immediately looking for the conditions:
[a-z]+\b Only alphabetic
\d+\b Only numeric
\w*(\d)\w*\1 Has a repeating digit
You can use this
\b(?!\w*(\d)\w*\1)(?=(?:[a-z]+\d+)|(?:\d+[a-z]+))[a-z0-9]+\b
\b - Word boundary.
(?!\w*(\d)\w*\1) - Condition to check unique digits.
(?=(?:[a-z]+\d+)|(?:\d+[a-z]+)) - Condition to check alphanumeric words.
[a-z0-9]+ - Matches a to z and 0 to 9
Demo
I have this ^[a-zA-Z0-9 #&$]*$, but not working for me in few cases.
If someone types
A string that only consists of digits (e.g. 1234567)
A string starting with a special character (e.g. &123abc)
need to be rejected. Note that a special char can be in the middle and at the end.
You seem to need to avoid matching strings that only consist of digits and make sure the strings start with an alphanumeric. I assume you also need to be able to match empty strings (the original regex matches empty strings).
That is why I suggest
^(?!\d+$)(?:[a-zA-Z0-9][a-zA-Z0-9 #&$]*)?$
See the regex demo
Details
^ - start of string
(?!\d+$) - the negative lookahead that fails the match if a string is numeric only
(?:[a-zA-Z0-9][a-zA-Z0-9 #&$]*)? - an optional sequence of:
[a-zA-Z0-9] - a digit or a letter
[a-zA-Z0-9 #&$]* - 0+ digits, letters, spaces, #, & or $ chars
$ - end of string.
you can do it with the following regex
^(?!\d+$)\w+\S+
check the demo here
I need regular expression which could match a string like that
_test
123test
test
test_123
test-123
123.a
I created this regular expression:
/^[_0-9a-z][_.\-a-z0-9]*$/
However, I want to exclude a string if it only contains numbers.
How can I fix it?
To avoid matching a digit-only string, add a negative lookahead:
^(?![0-9]+$)[_0-9a-z][_.\-a-z0-9]*$
^^^^^^^^^^
The (?![0-9]+$) lookahead is triggered only once at the beginning of the string and will try to match one or more digits up to the end of string. If they are found, a match will be failed (no match will be returned) as the lookahead is negative.
I need to validate building name in the below format (length is 1-50)
my regex for alphanumeric and specified characters check
/^[a-zA-Z0-9\s\)\(\]\[\._-&]+$/
Its showing invalid expression but when i exclude & it works fine.
/^[a-zA-Z0-9\s\)\(\]\[\._-]+$/
Actual Format
Building name should use Letters, Numbers, Underscore_, Hyphen-, Period., Square-brackets[], Parentheses() ,Ampersand &
It should not start and end with any special characters continuously
Valid:
Empire3 State&Building[A]
7Empire.State-Building(A)
Empire(State)_Building[A]12
Invalid:
##$#$#building))
().-building2
$buildingseven[0]&.
i am struggling for 2nd format. how to check for continuous allowed special characters at first and last. Any help is very much appreciated.
Escape the - character:
/^(?!\W.+\W$)[a-zA-Z0-9\s\)\(\]\[\._\-&]+$/
In a character class, the - character signifies a range of characters (e.g 1-9). Because the ASCII code for & is less than _, your regular expressions fails to parse correctly.
Also, to check that no special characters are at the beginning or end, use \W (a character other than a letter, digit or underscore) in a lookahead to check that both the start and the end are not "special characters". If you count an underscore as a special character, use [^A-Za-z0-9] instead of \W.
var validBuildingName = /^(?!\W.+\W$)[a-zA-Z0-9\s\)\(\]\[\._\-&]+$/;
validBuildingName.test('(example)'); // false
validBuildingName.test('(example'); // true
validBuildingName.test('example)'); // true
validBuildingName.test('example'); // true