inherit
241933
0
Feb 14, 2017 20:23:20 GMT -8
miminsy
3
February 2017
miminsy
|
Post by miminsy on Feb 14, 2017 9:50:12 GMT -8
This may not be entire related but I'm trying to find a regular expression that will pick up integer numbers ending with multiples of 25. e.g. 25, 50, 75, 100, 125, 150, 175, 200 etc.. you get the idea. However, the problem remains that the regex doesn't work and I'm a total noob at this This is the formula I found and edited a bit to try and make it work ^(?!0\d)+\b((25)|(50)|(75)|(00))$ But the problem is that... it only matches the expression if it's 25, 75, 50 or 00. I have no idea how to make it such that the digits before the last 2 are all accepted? It would be great if anyone could help me with this! Thank you so much!
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Feb 14, 2017 10:35:29 GMT -8
I personally wouldn't try and write a regular expression for this problem. I would instead use the modulo division operator.
Example:
var num = 25;
while(num <= 1000){ if(num % 25 == 0){ console.log(num); } num ++; }
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,846
January 2015
msg
|
Post by Lynx on Feb 14, 2017 11:23:18 GMT -8
If you're not familiar with modulus, it basically means that you want the remainder of a division. For example, 25/4 equals 6 with a remainder of 1. The remainder (1, in this case) is your modulus.
With Peter's example above, it's taking every number from 25 (initial value of num) to 1000, inclusive, (found in the while loop) and dividing that number by 25. If the remainder is 0, this means that the number is evenly divisible by 25 (since there is no remainder) - thus making it a multiple of 25.
|
|
Kami
Forum Cat
Posts: 40,198
Mini-Profile Theme: Kami's Mini-Profile
#f35f71
156500
0
Offline
Jul 24, 2021 11:48:29 GMT -8
Kami
40,198
July 2010
kamiyakaoru
Kami's Mini-Profile
|
Post by Kami on Feb 14, 2017 11:41:06 GMT -8
For some minor clarification, that's every number starting at 25 that is less than or equal to ( <= ) 1000 (so 1000 would be included in that). I'm sure that is what MSG meant but I wanted to make doubly sure it was understood.
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,846
January 2015
msg
|
Post by Lynx on Feb 14, 2017 13:51:17 GMT -8
Thanks, Kami. I edited my post above to add the word inclusive.
|
|
#00AF33
Official Code Helper
19529
0
1
Nov 19, 2012 14:18:28 GMT -8
Todge
**
17,324
January 2004
todge
|
Post by Todge on Feb 14, 2017 14:58:02 GMT -8
If you really want to use RegExp, try
^\d+(25|50|75|00)$
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Feb 14, 2017 15:41:10 GMT -8
If you really want to use RegExp, try ^\d+(25|50|75|00)$ Hate to nitpick but ^\d*(25|50|75|00)$ would be best as it would also match 25, 50, 75 and 00.
|
|
Former Member
inherit
guest@proboards.com
225992
0
Nov 23, 2024 10:13:20 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Feb 14, 2017 15:52:20 GMT -8
Those regular expressions are not greedy enough, so the modulo is a better implementation.
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Feb 15, 2017 14:19:09 GMT -8
The modulo cycles through each number in a loop and displays those that are multiples of 25. The RegEx would match numbers in already existing text such as post. If the whole post wanted to be scanned for matches then drop the start and end of string symbols and include the global flag like so.
/\d*(25|50|75|00)/g
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Feb 16, 2017 16:44:35 GMT -8
Both methods are a fine solution for the original question, as we do not know the input (poster didn't specify that). If it is a mixed value, then I agree that the regular expression would be better, otherwise modulo would be the better option for a single value type.
|
|
Former Member
inherit
guest@proboards.com
225992
0
Nov 23, 2024 10:13:20 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Feb 16, 2017 16:59:38 GMT -8
The modulo is used for Math(integer) calculations, is that not what the OP wanted? Or did I miss something?
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Feb 16, 2017 17:04:14 GMT -8
@synthtec, When I first read the question, I thought the same thing as you, hence the reason for not given a regular expression as a solution. Until the OP responds, guess we will never know
|
|
Former Member
inherit
guest@proboards.com
225992
0
Nov 23, 2024 10:13:20 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Feb 16, 2017 17:14:07 GMT -8
Well, both yourself Peter and Quozzo have given the correct solution to both, so the question was answered in my mind.
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,846
January 2015
msg
|
Post by Lynx on Feb 16, 2017 18:34:27 GMT -8
miminsy, Has any of the solutions above worked for you? If not, could you elaborate more on how you need it to function?
|
|