Regex to prevent double byte characters. Only allow ascii in a range - regex

I have a regex for password control. It will only allow alphanumeric characters and some symbols.
"^(?:(?=.*[a-z])(?=.*[#?!$%^&-*#0-9])(?=.*[A-Z]).{8,128})$$"
Now I want to prevent any double byte characters being used in the password.
I've tried having a list that only allows ascii but that didnt work ^[ -ࣿ]+$
Tried having a list in a range ?=.*[0x21-0x7e]
Would it be possible to flip the logic on my first regex so that if it was a character NOT included in the list it wouldn't allow entry?
I have discovered if I use only double byte it doesn't allow entry but it will if I include a single double byte. I don't want any.
This is in a finnicky scripting language nsis so cant rely on many functions just pattern matching .

The dot allows any character, so replace it with an ASCII range [!-z]:
^(?=.*[a-z])(?=.*[#?!$%^&*#0-9-])(?=.*[A-Z])[!-z]{8,128}$
Note that the backtick is included in this range, and tilda ~ is excluded.
To exclude backtick use [!-_a-z].
To include tilda, use [!-~].
To do both, use [!-_a-~].
Bug in symbol character class fixed: [#?!$%^&-*#0-9] should be [#?!$%^&*#0-9-]: you don’t want the range & to *, either escape the dash or put it first or last.
Unnecessary group removed.

Related

Regex ignoring hyphens and double hyphens

I am trying to get beyond compare ignore a single "-" and double "--" hyphen. I am comparing two PDF documents where the original has a single hyphen and the edited copy replaced all the single hyphens with doubles. I want Beyond Compare to ignore that difference.
I have searched and found where people are able to ignore after either a char or digit (below), but these may be preceded with a space?
[0-9,-]+
You could use the replacement setting (Session->Session Settings)
To do this, you don't need a regex, the replacement is done only in one side.

Remove space from regular expression

I do not want to show error when adding space. While using this regular expression, when adding space, shows error. How can I solve this?
return /^[A-Za-z\‘“:(),.$#!?=-_/\{}<>%^*]+$/.test(value);
I assume you mean that you want to allow spaces within value, right?
Then simply add a space to the character class. Make sure that the dash is never between two characters in the class, unless you want to specify a range (as you probably meant to do in A-Z but not in =-_). Also, this regex contains an error because you didn't escape the /:
return /^[- A-Za-z‘“:(),.$#!?=_\/{}<>%^*]+$/.test(value);
Add spaces using \s to the regex:
/^[-\sA-Za-z\‘“:(),.$#!?=_\/\{}<>%^*]+$/
As #Tim suggested below, / should be escaped and the - should not come between two characters for which it will define a range. Answer was updated respectfully.
Second, as was noted in the comments below, it's worth mentioning that \s matches all types of spaces (tabs, newlines etc) which is even more general than what the OP asked for

Regex for Alphanumeric characters, .#&,’()+/: and one hyphen only

I have a regex for matching letters, numbers and some special characters as follows: ^[A-za-z0-9 .#&,’()+/:]*$
I need to add a single hyphen to this list, not allowing multiple hyphens, but I'm not quite sure how to do it. I saw something along the lines of -{1} but I don't know how to add that to the existing rexex.
I'm using C++ and Qt5.
How about:
^[A-za-z0-9 .#&,’()+/:]*-?[A-za-z0-9 .#&,’()+/:]*$
that could be reduce to:
^[\w .#&,’()+/:]*-?[\w .#&,’()+/:]*$
I don't know if C++ support it, but it could be reduced to:
^([\w .#&,’()+/:])*-?(?1)*$
^[A-za-z0-9.#&,’()+/:]*-[A-za-z0-9.#&,’()+/:]*$ allows a single hyphen anywhere in the string.
Note that the hyphen may come at any part (at the beginning or end of the string also) and it is mandatory also.
To make the hyphen optional, use ^[A-za-z0-9.#&,’()+/:]*-?[A-za-z0-9.#&,’()+/:]*$

I need a regular expression First name must starts with minimum 2 characters

I need a regular expression First name must starts with minimum 2 characters.
I am using following
/^[A-Z a-z]{2,25}$/
Your regex appears to allow spaces so I'm not sure if that's valid.
If you want a regex that dictates at least two alpha characters at the start, you can just use:
/^[A-Za-z]{2}/
This will force the first two characters to be alpha with no restriction whatsoever on the rest of the string. If you want to (as it looks) allow those same character plus spaces for up to another 23 characters after that, use:
/^[A-Za-z]{2}[A-Za-z ]{0,23}$/
Otherwise, if your definition of characters includes a space even for the first two characters, your current regex should be fine.
The question is which regexp are you using? I assume POSIX RE.
Your solution is fairly good, but if You check a real name, the space is useless. (Maybe You are check for mind name as well???) And (I suppose) the first character should be a capital letter, rest lower case, isn't it? If so I suggest:
/^[[:upper:]][[:lower:]]{1,24}$/
The advantage of using built in character classes, it that it can work in other languages as well.
Example:
$ echo -e "Alpha\nalpha\naLpha\nThisisaverylongfirstname\nThisfirstnameismuchlongertobesure"\
|egrep '^[[:upper:]][[:lower:]]{1,24}$'
Alpha
Thisisaverylongfirstname

Regex to match non integers?

Trying to create a regex that ignores a proper integer (1, 5, 999, etc.) and forward slashes (/), but finds a match in everything else. For example, it would find a match the following:
test
test1
test-1
but ignores
1
55
7
This is for a mod rewrite.
[^-0-9\/]+ should do the trick, I think. It'll match any string that contains a non-digit.
Edited to add minus sign which would also be allowed in an integer, and then to include the forward slashes mentioned in the question.
This is a very old question, but I noticed that the currently accepted answer ([^-0-9\/]) will not match any part of a number string that has a dash/minus (-) in the middle or at the end or purely consists of dashes.
So the regex will not find a match in strings like 12-34, 1234-, --12, -, or -----, even though these are clearly not valid integer numbers and should thus be caught.
To include these in the regex, you can use something like this:
[^-0-9]+|[0-9]+(?=-)|^-$|-{2,}
This will match
either any part of a string that contains a non-digit, non-minus character (as does the regex from the currently accepted answer)
or any number that is followed by a minus sign - (using a positive lookahead, taking care of the numbers with dashes anywhere but the beginning)
or any string that consists of one single dash
or any part of a string with two or more consecutive dashes
This will thus find a match in any string that is not a positive or negative integer, see also https://regex101.com/r/8zJwCy/1
To also include (or rather exclude) slashes, as the OP requested, they can be added to the first character group.
[^-0-9\/]+|[0-9]+(?=-)|^-$|-{2,}
Note that this will also not match pure series of slashes (/, //, ///,...) and multiple slashes between integers (1//2, 1//-2, //1, 2//) which may or may not be desired (the currently accepted answer behaves identically), see https://regex101.com/r/8zJwCy/3
To also catch these, add another two optional groups that match series of slashes, analogous to the last two ones that take care of the dashes. Or, if you want a single individual slash (/) to be valid, just add one group analogous to the last one, see https://regex101.com/r/8zJwCy/4
[^-0-9\/]+|[0-9]+(?=-)|^-$|-{2,}|\/{2,}
This would match line by line - is that what you need?
^[0-9\/]*$
This should do the job!
^[0-9\/]+$