Regex for float matching strings like "12." also? - javascript

I tried with this regex to match floating values:
(^\d{0,11}$)|^\d{0,11}([.]\d{0,6})?$
However, I don't want to allow strings like 12., i.e. a number with a dot at the end.
Please me give me a suggestion.

You may use
^\d{0,11}(?:\.\d{1,6})?$
If you use \d{0,6}, the pattern may match an empty string. Note that it is not recommended to test JS regex with RegexStorm.net as it only supports .NET regex and uses CRLF line endings.
Details
^ - start of string
\d{0,11} - zero to eleven digits
(?:\.\d{1,6})? - an optional sequence of
\. - a dot
\d{1,6} - 1 to 6 digits
$ - end of string.
See the regex demo.

How about you restrict the count of the decimal part.. like this
^\d{0,11}(?:\.\d{1,6})?$

Related

Regexp to validate ip address with last digit as non zero

I have a regex that i ended up using from one of the answer here in SO .
Basically my regex must validate ipv4 address with mask .
So i ended up using the below regex :
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254))))
Now my challenge is to not allow 0 in the last digit of ip i.e ,
192.168.6.10/mask is valid but 192.168.6.0/mask is invalid
So i modified the above regexp to something like this :
(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[1][0-9][0-9]|[1-9][0-9]|[1-9]?)/([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|(255\.(0|128|192|224|240|248|252|254)\.0\.0)|(255\.255\.(0|128|192|224|240|248|252|254)\.0)|(255\.255\.255\.(0|128|192|224|240|248|252|254))))
but 192.168.6.0 is always valid when testing with Angular Validators.pattern
Any idea where i'm going wrong ?
EDIT
List of IPs & its validity :
192.168.6.6/24 is valid
192.168.6.6/24 is valid
192.168.6.24/24 is valid
192.168.6.0/24 invalid
192.168.6.0/255.255.255.0 is invalid
You want to avoid matching any IP with the last octet set to 0.
You may use
ipAddress : FormControl = new FormControl('' , Validators.pattern(/^(?!(?:\d+\.){3}0(?:\/|$))(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:[1-9]|1[0-9]|2[0-9]|3[0-2]|(?:(?:128|192|224|240|248|252|254)\.0\.0\.0|255\.(?:0|128|192|224|240|248|252|254)\.0\.0|255\.255\.(?:0|128|192|224|240|248|252|254)\.0|255\.255\.255\.(?:0|128|192|224|240|248|252|254)))$/));
Here is the regex demo
The main addition is the lookahead after ^ that is executed once at the start of a string. The (?!(?:\d+\.){3}0(?:\/|$)) pattern is a negative lookahead that fails the match if, immediately to the right of the current location (string start), there are:
(?:\d+\.){3} - three repetitions of 1+ digits and a dot
0 - a zero
(?:\/|$)) - / or (|) end of string ($).
Notice I defined the pattern using a regex literal notation (/regex/) and I had to add ^ (string start) and $ (string end) anchors since the regex was no longer anchored by default. Also, to escape special chars in a regex literal notation, you only need one backslash, not two.
Suppose that the last part cannot be written 000 and 00 but just 0. Then you can you such regex
^(?:(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)\.){3}(?:(?:2(?:5[0-5]|[0-4]\d)|1?\d\d|[1-9]))$
Where diff between the first groups and the last one that one-digit value should be from 1 to 9
demo
You can try with this pattern
^(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:[1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(?:2[0-5][1-5]|[1-9]|1[0-9][1-9]|[1-9][1-9])$
Online demo
For the last numbers you have check with this
(?:2[0-5][1-5]|[1-9]|1[0-9][1-9]|[1-9][1-9])
One possible approach here is simple, and just involves adding a negative lookbehind at the very end of the pattern (?<!\.0), which asserts that .0 is not the immediately preceding term in the IP address. Applying this to your correctly working pattern from the comments above, we get:
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/
([1-9]|1[0-9]|2[0-9]|3[0-2]|(((128|192|224|240|248|252|254)\.0\.0\.0)|
(255\.(0|128|192|224|240|248|252|254)\.0\.0)|
(255\.255\.(0|128|192|224|240|248|252|254)\.0)|
(255\.255\.255\.(0|128|192|224|240|248|252|254))))(?<!\.0)$
Demo
The downside is that your JavaScript engine may not yet support negative lookbehind syntax just yet.

Regex to allow alphanumeric, spaces, some special characters

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

Matching a capturing group or failng the match if missing in Javascript regex

I am trying to grab a digit (5) followed by a possible - or (space) proceeding a sequence of 3 digits, followed finally by a single digit. If the first group doesn't match at all, then only return the other sequences of digits.
^(5\-? ?)?(\d{3})(\d)$
This looks right to me and doesn't throw any errors, but it's giving the 5 back:
"5489" -> ()("548")("9")
Where I would actually not want this expression to return a match for this pattern.
So a quick search brought me to possessive expressions and a lot of articles about your ex. From what I'm reading, this looks like it should work:
^(5\-? ?)?+(\d{3})(\d)$
But Javascript does not like that as a regular expression.
Is there a way to do a greedy possessive capture group in Javascript, or simulate it in this situation?
You can simulate possessive quantifier functionality by taking advantage of lookaround qualities:
^(?=(...))\1
Regex:
^(?=((5\-? ?)?))\1(\d{3})(\d)$
Live demo
If you have a look at the ECMAScript 5 docs, you will see there is no support for possessive quantifiers.
You may use
^(?!5\d{3}$)(5-? ?)?(\d{3})(\d)$
See the regex demo. The (?!5\d{3}$) negative lookahead will fail a match at once if the string starts with 5 and then has 3 digits.
Details:
^ - start of string
(?!5\d{3}$) - there cannot be 5 and then 3 digits and the end of string immediately to the right of the current location
(5-? ?)? - an optional sequence of 5, then an optional - and then an optional space
(\d{3}) - 3 digits
(\d) - one digit
$ - end of string.

.net Regex for more than 5 consecutive alphabet letters

I am attempting to write a .Net Regex for more than 5 consecutive alphabet letters.
dfhjvudyfyreaaaaa - not allowed
dfhjvudyfyreAAAAA - not allowed
dfhjvAAAAAfyreAAA - not allowed
dfhjvAAAfyresdAAA - allowed
dfhjvAAAf3434yresdA - allowed
So far, i have tried some reg exp but, not working as per my requirement.
My requirement are below
String length should be less than 25 character
String is Combination of alphabet/number only, not allow special character.
Solution tried
^(?=.{1,25}$)(([a-zA-Z0-9])\5?(?!\5))+$
(?i)(.)\1\1
I will used string to represent vehicle engine number or chassis number.
I am okey, if there possible solution in JavaScript.
Can we build this type of regex in .net?
With the case insensitive flag, you can write it using a negative lookahead to test if there isn't five consecutive characters:
^(?!.*(.)\1{4})[A-Z0-9]{1,25}$
This is a possible solution:
^(?![a-zA-Z0-9]*([a-zA-Z0-9])\1{4})[a-zA-Z0-9]{1,24}$
See the regex demo.
Details
^ - start of string
(?![a-zA-Z0-9]*([a-zA-Z0-9])\1{4}) - no 5 consecutive alphanumeric chars anywhere inside the string
[a-zA-Z0-9]{1,24} - 1 to 24 alphanumeric chars
$ - end of string.
If you set a case insensitive flag, you will make the backreference also case insensitive (aaAAA will also fail the match). In .NET, you may pass the flag as RegexOptions.IgnoreCase option, and in JS, add i modifier.
C# implementation:
bool valid = Regex.IsMatch(input, #"^(?![a-z0-9]*([a-z0-9])\1{4})[a-z0-9]{1,24}\z", RegexOptions.IgnoreCase);
I suggest \z in .NET because it always matches at the very end of the string while $ can match before a final newline char (not the case in JS).
In JS, you may use
var valid = /^(?![a-z0-9]*([a-z0-9])\1{4})[a-z0-9]{1,24}$/i.test(s)

Regex not matching 6 repeated numbers

I am trying to get a regular expression to work but am stumped. What I want is to do the inverse of this:
/(\w)\1{5,}/
This regex does the exact opposite of what I'm trying to do. I would like to get everything but a string that has 6 repeating numbers i.e. 111111 or 999999.
Is there a way to use a negative look-around or something with this regex?
You can use this rgex:
/^(?!.*?(\w)\1{5}).*$/gm
RegEx Demo
(?!.*?(\w)\1{5}) is a negative lookaahead that will fail the match if there are 6 consecutive same word characters in it.
I'd rather go with the \d shorthand class for digits since \w also allows letters and an underscore.
^(?!.*(\d)\1{5}).*$
Regex explanation:
^ - Start of string/line anchor
(?!.*(\d)\1{5}) - The negative lookahead checking if after an optional number of characters (.*) we have a digit ((\d)) that is immediately followed with 5 identical digits (\1{5}).
.* - Match 0 or more characters up to the
$ - End of string/line.
See demo. This regex will allow

Categories