1

I made this one for 0.1 to 8.9

^(?:8(?:\.0)?|[1-8](?:\.[0-9])?|0?\.[1-9])$

^                  # Start of string
(?:                # Either match...
 8(?:\.0)?         # 8.0 (or 8)
|                  # or
 [1-8](?:\.[0-9])? # 1.0-8.9 (or 1-8)
|                  # or
 0?\.[1-9]         # 0.1-0.9 (or .1-.9)
)                  # End of alternation
$                  # End of string

My question is that this works: **[1-8]**(?:\.[0-9])? # 1.0-8.9 (or 1-8) how do i make it work for 1-29 because the below one does not work.

 **[([1-9]|1[0-9]|2[0-9])]** : this is for `1-29`.

Doesn't work.

How do i do it?

Akshit
  • 23
  • 5
  • What is your use case? Sounds like matching all numbers and working with resultant array would be more practical....or using replace callback or whatever depending on usage – charlietfl Jul 11 '18 at 12:09
  • 1
    The square brackets are for matching a *character set*. You can't use them to identify something that is not a list of characters, like you're doing. So: `[1-99]` won't work since '99' is not a character; either using `[([1-9]|1[0-9]|2[0-9])]` since you're put inside the brackets other character sets. For example, if you want to match `1-29`, you have to write something like: `^[0-2]?[0-9]$`. – ZER0 Jul 11 '18 at 12:16
  • `^(?!0\.0)(0\.[1-9]|[1-2]?[0-9]\.[0-9]|30\.0)$` should work. As pointed out above, something like `[0-10] ` doesn't work so you need to make something a little uglier to get around this. – James Whiteley Jul 11 '18 at 12:38
  • i already mentioned in question what works for 1-29, but it doesn't work when i put in the bracket for my regular expression. I need to know the fix for that. [1-8](?:\.[0-9])? # 1.0-8.9 (or 1-8) this works. but this does not. [^[0-2]?[0-9]$](?:\.[0-9])? – Akshit Jul 12 '18 at 05:44

1 Answers1

0

There are better ways to achieve your goals like simple number comparison, but here is what you asked for:

/^[12]{1}[0-9]?\.?(?(?<=\.)(?:[0-9]{1,2}$)|(?:$))|0\.?(?(?<=\.)(?:[0-9]{1,2})|(?:$))|(?:30\.0|30)$/

Regexp is using RegExp Lookbehind Assertions which is part of ES 2018 specification.

Moonjsit
  • 630
  • 3
  • 11
  • This does not work. https://regex101.com/r/bY1yT2/2 – Akshit Jul 12 '18 at 05:40
  • You used a different regular expression (`/^(?:[1-9][0-9]{0,4}(?:\.\d{1,2})?|100000|100000.00)$/`). Additionaly environement is set to PHP not JS. In order to test strings on regex101 you need to add test cases [in tests tab](https://regex101.com/r/bY1yT2/2/tests). – Moonjsit Jul 12 '18 at 12:09
  • I need to use it in json, not js. It gives pattern error no matter what you use. – Akshit Jul 13 '18 at 07:04