Regex: 15 integers with a maximum of two decimals, excluding 0 [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to create a custom validation with regex but cant find the right one.
15 integers max, 2 decimals max. 0 is not allowed.
Im usign this regex at this moment: /^(?:\d{1,15}(?:[.,]\d{0,2})?|[.,]\d{1,2})$/
but that one stills allows a 0
Valid cases:
0,01
123,1
1234,50
123456789012345,20
invalid cases:
0
-1
13,421
123,223
1234567890123456

The below pattern matches a digit between 1 and 15 times, followed by an optional group comprising a comma then either one or two digits. The pattern matches the entire string (from start to end) due to the anchors. It begins with a negative lookahead to ensure the entire string is not just the character "0".
(?!^0$)^\d{1,15}(?:,\d{1,2})?$
It matches all valid cases and no invalid cases from your question.
Try it out here: https://regex101.com/r/kB8jXt/1

Related

regex creation with multiple conditions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
i need a regex that fits these conditions
may contain letters and numbers. numbers are optional but must contain at least 1 letter
at least 2 characters
can contain ONLY the "-" character of special characters. this is optional
must begin with a letter
no whitespace
no turkish characters
How should i create a regex query according to these conditions?
I would create a function like so:
function isGood(string){
return /^[a-z][a-z0-9-]+$/i.test(string);
}
console.log(isGood('This should return false'))
console.log(isGood('#no'));
console.log(isGood('Nice'));
console.log(isGood('a'));
console.log(isGood('a!'));
console.log(isGood('great'));
console.log(isGood('This-should-also-pass-the-test'));

Regex match for several similar string formats [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What is the best way to match a set of strings in this format:
AA99999999 2 fixed characters 8 numbers
ABC99999999 3 fixed characters 8 numbers
AD999999999 2 fixed characters 9 numbers
also
BA999999999 2 fixed characters 9 numbers
BB99999x999 2 fixed characters 5 numbers 1 character 3 numbers
BHA-9999#9 4 fixed characters 4 numbers 1 fixed character 1 number
BHGD99999 4 fixed characters 5 numbers
For the first match I wrote a regex like this
[a-zA-Z]{3}\d{8}|([a-zA-Z]{2}\d{9}|[a-zA-Z]{2}\d{8})
This works but I have no idea about the second. Is there are regex that will match these formats?
It's pretty hard to tell exactly what you want here, but I've combined them into two regex expressions rather than using 4, because it's faster. I've used either \w or . where you used [a-zA-Z] because, again, it's difficult to tell what you want. Provide more information and I'll modify the answer.
\w{2}\d{5}.\d{3}|.{4}\d{4}.?\d
They're simply more of the same:
([a-zA-Z]{2}\d{9}|([a-zA-Z]{2}\d{5}[a-zA-Z]\d{3}|[a-zA-Z-]{4}[a-zA-Z#]\d)
assuming where a '-' pops up a [a-zA-Z-] is the group of values that can match there and likewise for [a-zA-Z#] for the '#'
I would take fixed characters to mean that the specification is to have a case where if the first 3 characters are ABC followed by 8 numbers instead of widlcard letters in which case the patterns for each case would be as follows:
AA99999999 2 fixed characters 8 numbers
AA\d{8}
AD999999999 2 fixed characters 9 numbers
AD\d{9}
ABC99999999 3 fixed characters 8 numbers
ABC\d{8}
also
BA999999999 2 fixed characters 9 numbers
BA\d{9}
BB99999x999 2 fixed characters 5 numbers 1 character 3 numbers
BB\d{5}x\d{3}
BHA-9999#9 4 fixed characters 4 numbers 1 fixed character 1 number
BHA-\d{4}#\d
BHGD99999 4 fixed characters 5 numbers
BHGD\d{5}
var patt1 = /^\w\w[\w\d]{1}[\d\w-]{1}\d{2}[\d\w]{1,4}[\d#]{1}\d+$/;
If this is the case you will need to either put each of these in as alternatives or do some logic matching or iterate through each of them to test until a match is found.
Or do you mean capital letters instead of fixed characters?
Maybe you could play with this fiddle.
https://jsfiddle.net/shotgundriver/r812c00v/6/
Is this a homework problem or real world?

Need a regular expression to match a substring n times [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
The regex .*{n} will match any single character n times, but I need to match any single substring n times.
How do I do that?
To match the substring "foo" 3 times (for example "foofoofoo"), you could use the following:
(foo){3}
Or with a non-capturing group:
(?:foo){3}
As a side note, .*{n} wouldn't do what you think it does. The . will match any character, .* will match any number of any characters, and .*{n} will vary depending on the implementation but it will either be an invalid regex, be equivalent to .*, or match any number of any characters followed by the literal string '{n}'.
Try
(your sub string here){n}
e.g.
(cats){4}
try
/(\w+)\1{n-1}/
Example:
"abcbcbca".match(/(\w+)\1{2}/) if you wish to find bc being repeated 3 times.
If you are trying to match a given string repeated n times, just do (string){n}.

how to get length of substring in Regular Expression JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
here is my regular expression
/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/
There will be one condition: the length of every string after first 40 characters must be 16 characters. This 16 characters string will be non repeating and minimum of 2 times and maximum of 10 times. So i want to get the length of this sub-string which should be 16.
Here is input string:
string input="PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522"
I want to match the length of "GEX2421262506027" and "GEX2437345435522". Like this, there will be 10 strings max. I want to validate if this length should be 16 characters.
try this
var pattern = /^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/;
var exp = text.match(pattern);
if (exp) {
alert(exp[0].length);
}
if you just want the last 16 characters:
var string = 'PR212D4EB2-6B25-4020-BD2A-941F69C0AA4102GEX2421262506027GEX2437345435522';
var matching = string.match(/^[a-zA-Z0-9-]{40}([a-zA-Z0-9-]{3}2[a-zA-Z0-9-]{12}){2,10}$/);
console.log(matching[1]);
console.log(matching[1].length);

help with regular expressions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is my goal numbers from the three digits to 9 digits. for example
Valid options
175
1.250
14.365
145.985
1.562.745
17.487.984
999.999.999
Now this is the regular expression that i develop
/^\d{1,3}\.\d{1,3}\.\d{1,3}$/
My problem it's that this is accepting this values
176.57.117 <---- this is not valid value
176.257.7 <---- this is not valid value
176.257.17 <---- this is not valid value
Thanks for your help
UPDATE
I'm trying to make a regular expression that validates positive natural numbers from three digits to 9 digits and separates the thousand unit and the million unit with a point
/^\d{1,3}(\.\d{3}(\.\d{3})?)?$/
What you really want is 1 to 3 digits possibly followed by 1 or 2 additional sets of three digits. Your original reg-ex just said "3 sets of 1-3 digits" which isn't really what you want. It also would have failed to accept your first several valid examples since they had less than three sets of digits.
Just split the string for . and then check string length ... in each of array indexes (i think it will be more self explaining than regexp)

Categories

Resources