Regex to restrict characters other than numbers, - and - regex

This is my regex which restricts any characters other than numbers -(minus) and .(dot)
Regex is as follows
(?!^-)[^0-9\.]
which allows - (minus) only at the start, But i would like to know If i can allow .(dot) only once and restrict the user to type the next .(dot)
My code
oninput => "this.value = this.value.replace(/(?!^-)[^0-9\.]/g,'')"

You want to allow:
Optional leading minus (-?)
At least one digit (\d+)
Optional suffix: a dot, followed by at least one digit (\.\d+)
An appropriate regex is:
^-?\d+(?:\.\d+)?$

As I understand, the user is to be restricted to entering the string representation of an integer or float (possibly negative), and that restrictions are to be imposed on a character-to-character basis. You therefore need a sequence of dynamically-constructed regular expressions that each match a single-character string.
I assume the user enters RETURN to signal that no more characters are to be entered. The user is to be permitted to enter RETURN only when the previous entry was a digit.
Define r = /[-\d]/.
The first character entered must match r.
If the first character entered is '-', set r = /\d/, else (as the first character was a digit) set r = /[\d.]/.
If the previous character was a period update r = /\d/ and require the user to enter a character that matches r (i.e, a digit); else the user may press RETURN or enter a character that matches r (which could be /\d/ or /[\d.]/).
Repeat step 4 until the user presses RETURN.
Note that, in step 4, once r is set /\d/ it remains at that value until the user presses RETURN.
If a user entry (including RETURN) is rejected it would be helpful to display a message giving the reason for it being rejected.

Related

Regular Expression for the Pattern?

I'm required to write a regular expression that has the following rules:
Digits between 1 to 4
hyphen (only one and can occur at any position)
Length of Text must be less than or equal to 6 (including the potential hyphen)
May end with a letter or a number, but not a hyphen.
Some valid examples are:
1-3411
12-413
123-2A
11-1
These examples are invalid:
12--11 ( since it contains two hyphens)
1-2345 ( since it contains number 5)
11-2311 ( since length is more than 6)
The RegEx that I wrote is:
^[1-4]-[1-4]{4}|^[1-4]{2}-[1-4]{3}|^[1-4]{3}-[1-4]{2}|^[1-4]{4}-[1-4]
However, this does not seem to be working, and it doesn't handle the case of a single character being is present in the end.
Can some some please help me determine a way of handling this?
<>
is character occurs in last position then before character we must have a digit not hypen .
i.e 11-a ( must fail)
11-1a (must pass)
^(?!(?:[^-\n]*-){2})(?:[1-4-]{1,5}[1-4]|[1-4-]{1,5}[a-zA-Z])$
You can handle that using a lookahead.See demo.
https://regex101.com/r/tS1hW2/16
If you have such a complex requirement, it is always easy to use lookarrounds to form an and-pattern matching each condition at the same time. Sometimes you need to split up ONE condition into two:
Base-Match: 6 or less digits: ^.{1,6}$
(AND) Only 1-4 and hyphen and letter: ^[1-4a-z\-]+$ (not accurate, requires next line)
(AND) First 1...5 elements NO Letter: ^[1-4\-]{1,5}[1-4a-z]$
(AND) No double hypen and not at the end: ^[^-]*-[^-]+$
Putting all together leads to:
(?=^[1-4\-]{1,5}[1-4a-z]$)(?=^[^-]*-[^-]*$)(?=^[1-4a-z\-]+$)^.{1,6}$
Debuggex Demo

blank spaces, number must start with +

I need to get a regex where a phone number must begin with a +. There can be a comma seperated list eg
List:
tel1: +E1234498912345678#fake.com, tel2: +498912345678, tel1: +E123449D1238912345678#fake.com
is a valid list. E is a valid special case
My regex is this:
^(tel1:)|(tel2:)( )(\+.)$
but it accepts numbers without a + as being valid which is not what I want. The number MUST be preceded by a + otherwise it's invalid. Any hints?
You can try the following:
(tel[12]:\s*\+[eE]?\w+(#\w+(\.\w+)+)?(,\s*)?)+
This should match telephone numbers separated by a comma, in a single line.
It also oversees the use of a special character E or e.
Also, domains may not only end in .com. .net or .com.uk should also be valid.

Matching Regular expression any out of four

I am new in regular expression.
I want to validate my password which must contains any three from below:
One digit
one Lower letter
one Upper case Letter
One special letter from this set of characters
.~^;:?=##${}|_()*,-
If any user enters One digit, One lower case letter, One Upper case letter and Special letter not from above group i.e 1234S%n&, expression should return false.
I have tried below expression :
(?=.*\d)(?=.*[A-Z])(?=.*[a-z]).*$|(?=.*\d)(?=.*[A-Z])(?=.*[.~^;:?=##${}|_()*,-])(?!.*[+&%<>]).*$|(?=.*\d)(?=.*[a-z])(?=.*[.~^;:?=##${}|_()*,-])(?!.*[&+%<>]).*$|(?=.*[A-Z])(?=.*[a-z])(?=.*[.~^;:?=##${}|_()*,-])(?!.*[&+%<>]).*$
Please help me to solve my confusion
Match "at least n criteria, any of the following" is not very easy to do in a single regex.
I would recommend against it, and doing it in multiple steps (quick&dirty pseudo code):
password = "1234S%x"
i = 0
// only allowed characters are in the string
char_validated = password.find("^[a-zA-Z0-9.~^;:?=##${}|_()*,-]+$")
if password.find("[0-9]") then i++ // check for at least one digit
if password.find("[a-z]") then i++ // check for at least one lowercase
if password.find("[A-Z]") then i++ // check for at least one uppercase
if password.find("[.~^;:?=##${}|_()*,-]") then i++ // check for at least one special
if (i>=3 && char_validated) then ok
If you really need to do it in one regex, you could use your refactored not-easy-on-the-eye regex:
^(?:(?=.*\d)(?:(?=.*[A-Z])(?=.*[a-z])|(?=.*[A-Z])(?=.*[.~^;:?=##${}|_()*,-])|(?=.*[a-z])(?=.*[.~^;:?=##${}|_()*,-]))|(?=.*[A-Z])(?=.*[a-z])(?=.*[.~^;:?=##${}|_()*,-]))[a-zA-Z0-9.~^;:?=##${}|_()*,-]+$
See demo here.
The idea is, instead of allowing anything after checking with lookaheads, to force the allowed characters with [a-zA-Z0-9.~^;:?=##${}|_()*,-]

Regular expression for password (at least 2 digits and one special character and minimum length 8)

I have been searching for regular expression which accepts at least two digits and one special character and minimum password length is 8. So far I have done the following: [0-9a-zA-Z!##$%0-9]*[!##$%0-9]+[0-9a-zA-Z!##$%0-9]*
Something like this should do the trick.
^(?=(.*\d){2})(?=.*[a-zA-Z])(?=.*[!##$%])[0-9a-zA-Z!##$%]{8,}
(?=(.*\d){2}) - uses lookahead (?=) and says the password must contain at least 2 digits
(?=.*[a-zA-Z]) - uses lookahead and says the password must contain an alpha
(?=.*[!##$%]) - uses lookahead and says the password must contain 1 or more special characters which are defined
[0-9a-zA-Z!##$%] - dictates the allowed characters
{8,} - says the password must be at least 8 characters long
It might need a little tweaking e.g. specifying exactly which special characters you need but it should do the trick.
There is no reason, whatsoever, to implement all rules in a single regex.
Consider doing it like thus:
Pattern[] pwdrules = new Pattern[] {
Pattern.compile("........"), // at least 8 chars
Pattern.compile("\d.*\d"), // 2 digits
Pattern.compile("[-!"ยง$%&/()=?+*~#'_:.,;]") // 1 special char
}
String password = ......;
boolean passed = true;
for (Pattern p : pwdrules) {
Matcher m = p.matcher(password);
if (m.find()) continue;
System.err.println("Rule " + p + " violated.");
passed = false;
}
if (passed) { .. ok case.. }
else { .. not ok case ... }
This has the added benefit that passwort rules can be added, removed or changed without effort. They can even reside in some ressource file.
In addition, it is just more readable.
Try this one:
^(?=.*\d{2,})(?=.*[$-/:-?{-~!"^_`\[\]]{1,})(?=.*\w).{8,}$
Here's how it works shortly:
(?=.*\d{2,}) this part saying except at least 2 digits
(?=.*[$-/:-?{-~!"^_[]]{1,})` these are special characters, at least 1
(?=.*\w) and rest are any letters (equals to [A-Za-z0-9_])
.{8,}$ this one says at least 8 characters including all previous rules.
Below is map for current regexp (made with help of Regexper)
UPD
Regexp should look like this ^(?=(.*\d){2,})(?=.*[$-\/:-?{-~!"^_'\[\]]{1,})(?=.*\w).{8,}$
Check out comments for more details.
Try this regex. It uses lookahead to verified there is a least two digits and one of the special character listed by you.
^(?=.*?[0-9].*?[0-9])(?=.*[!##$%])[0-9a-zA-Z!##$%0-9]{8,}$
EXPLANATION
^ #Match start of line.
(?=.*?[0-9].*?[0-9]) #Look ahead and see if you can find at least two digits. Expression will fail if not.
(?=.*[!##$%]) #Look ahead and see if you can find at least one of the character in bracket []. Expression will fail if not.
[0-9a-zA-Z!##$%0-9]{8,} #Match at least 8 of the characters inside bracket [] to be successful.
$ # Match end of line.
Regular expressions define a structure on the string you're trying to match. Unless you define a spatial structure on your regex (e.g. at least two digits followed by a special char, followed by ...) you cannot use a regex to validate your string.
Try this : ^.*(?=.{8,15})(?=.*\d)(?=.*\d)[a-zA-Z0-9!##$%]+$
Please read below link for making password regular expression policy:-
Regex expression for password rules

Regular expression to match IRC nickname

How would I use a regular expression to match an IRC nickname? This is being done in Ruby if that makes a difference (it probably will, with the syntax of the regex, but who knows.)
EDIT: An IRC nickname can contain any letter, number, or any of the following characters: <
- [ ] \ ^ { }
# If you are testing a single string
irc_nick_re = /\A[a-z_\-\[\]\\^{}|`][a-z0-9_\-\[\]\\^{}|`]*\z/i
# If you are scanning them out of a larger string
irc_nick_re = /(?<=[^a-z_\-\[\]\\^{}|`])[a-z_\-\[\]\\^{}|`][a-z0-9_\-\[\]\\^{}|`]*/i
The above allows single-character names. If two characters are required, change the * to +. If three characters (or more) are required, change it to {2,}, where '2' is the minimum number of characters minus 1.
If there is a maximum number of characters (for example, EFNet only allows nicknames up to 9 characters lone, while Freenode allows nicks up to 16 characters long) then you can include that number (minus 1) after the comma. For example:
# Validate nicknames that are between 3 and 16 characters long (inclusive)
irc_nick_re = /\A[a-z_\-\[\]\\^{}|`][a-z0-9_\-\[\]\\^{}|`]{2,15}\z/i