If the digits, letter and hyphen can appear mixed together in any order (like 1234k56-78, use a regex like this:
(?=.*[A-Za-z])(?=.*-)(?=(?:.*\d){8}).{10}
If the digits need to all be consecutive, then use a regex like this:
(?=.*[A-Za-z])(?=.*-)(?=.*\d{8}).{10}
Answer from coffee-converter on Stack OverflowIf the digits, letter and hyphen can appear mixed together in any order (like 1234k56-78, use a regex like this:
(?=.*[A-Za-z])(?=.*-)(?=(?:.*\d){8}).{10}
If the digits need to all be consecutive, then use a regex like this:
(?=.*[A-Za-z])(?=.*-)(?=.*\d{8}).{10}
Here's my suggestion:
s = "1239123-a4";
doesitmatch = s.split('').sort().join('').match(/^-[0-9]{8}[a-zA-Z]$/);
This sorts the string in a way that makes matching easier.
I think this is simple and it works:
String regEx = "^[0-9]{8}$";
^- starts with[0-9]- use only digits (you can also use\d){8}- use 8 digits$- End here. Don't add anything after 8 digits.
Your regex will match 8 digits anywhere in the string, even if there are other digits after these 8 digits.
To match 8 consecutive digits, that are not enclosed with digits, you need to use lookarounds:
String reg = "(?<!\\d)\\d{8}(?!\\d)";
See the regex demo
Explanation:
(?<!\d)- a negative lookbehind that will fail a match if there is a digit before 8 digits\d{8}8 digits(?!\d)- a negative lookahead that fails a match if there is a digit right after the 8 digits matched with the\d{8}subpattern.
You need to add the start-of-line and end-of-line markers to make sure only this pattern matches.
/^[\d]{4}-?[\d]{4}$/
Use ^ and $:
'00000000'.match(/^\d{4}-?\d{4}$/) // true
'0000-0000'.match(/^\d{4}-?\d{4}$/) // true
'000000000'.match(/^\d{4}-?\d{4}$/) // nil
^ matches the beginning of the string; $ matches the end. Without them, you're matching any substring. So your original regex would match a 20-digit string, for example.
In TypeScript, so you can see how the value types change (as you're performing String operations on a Number type!)
const someNumber: number = 123;
const asText : string = someNumber.toString();
const padded : string = asText.padEnd( 8, '9' );
const asNumber : number = parseInt( padded, 10 ); // ALWAYS specify the radix!
As a function:
function padNinesNumber( value: number ): number {
const asText : string = value.toString();
const padded : string = asText.padEnd( 8, '9' );
const asNumber : number = parseInt( padded, 10 ); // ALWAYS specify the radix!
}
I'm not sure if this is what you mean,
Try this, I gave the code to add '9' for every difference of 8 with the number of char in the string.:
const addNine = val =>
val.toString()+'9'.repeat(8-val.toString().length);
console.log(addNine('2233'))
console.log(addNine('012303'))
console.log(addNine(1230))
or you can try this if you're not giving number data type
console.log(('2233').padEnd(8,'9'))
console.log(('012303').padEnd(8,'9'))
Try this regex:
^[+-]?\d*\.\d{1,8}$
Click for Demo
Explanation:
^- asserts the start of the line[+-]?- matches either+or-, optionally\d*- matches 0+ occurrences of a digit. This represent the digits before the decimal part.\.- matches the decimal point.\d{1,8}- matches a minimum of 1 digit and a maximum of 8 digits$- asserts the end of the line
First of all, your current regex accept up to 9 decimals. I have adapted it to have maximum 8 digits after the dot.
^-?(?:(?:0|[1-9][0-9]*)(?:.[0-9]{1,8})?|.[0-9]{1,8})$
^ ^
^ ^
I have tested it here and you can see the list of matches: https://regex101.com/r/SQYgq9/1/
1
1234
1234.1234
123456.123456
1234567.1234657
12345678.12345678
12345678.1243546788 <NOT MATCHED
.91
0.12
.00001
.000001
.00000001
.000000001 <NOT MATCHED
.000000000 <NOT MATCHED
.0000000001 <NOT MATCHED
-1
-1234
-1234.1234
-123456.123456
-1234567.1234657
-12345678.12345678
-12345678.1243546788 <NOT MATCHED
-.91
-0.12
-.00001
-.000001
-.00000001
-.000000001 <NOT MATCHED
-.0000000001 <NOT MATCHED
If you still have issues for the input validation, you might have other problems like input not trimmed for example... From a regex point of view it is fine.
You can use this regex with optional - after each digit:
^([0-9]-?){8}$
If your regex supports \d then use:
^(\d-?){8}$
RegEx Demo
You should use
^0-9{7}$
^([0-9]-?){8}\b$
See the regex demo #1 and regex demo #2, where \b is used to make sure the last char is a digit (that is a word char).
Details
^- start of string[0-9]to match a digit since\din various regex flavors may match more than just ASCII digits from0to9.(-?[0-9]){7}- matches 7 sequences of an optional hyphen and a digit, and will not allow trailing hyphen at the end of the string.([0-9]-?){8}- matches eight occurrences of a digit followed with an optional-char\b$- is a trick to make sure the last char is of a word type. Since the pattern can only match a-(a non-word char) or a digit at the end,\bautomatically makes sure the last char is a digit.