-1

I need to write a regex to match number greater than or equal one. I have looked at this answer

Regex: ^(?:[2-9]|\d\d\d*)$

But, I do not get why there is ?: before the number? Also, if the number in my case is in the middle of sentence, then I should remove the following ^( )$ from my regex?

EDIT:

I need grate than or equal 1 not just greater than. So I should use [1-9] instead of [2-9]?

EDIT2: I need a regex to capture any number greater than or equal to the number 1. Example: 2,3,4, 11, 100, 31557600 or any number greater than that.

user9371654
  • 2,160
  • 16
  • 45
  • 78
  • 1
    `?:` indicates a non-capturing group. Read more: https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-what-does-do – BlackPearl May 09 '19 at 17:10
  • 2
    You can use word boundaries if you want to match numbers in a larger text and use `\b(?:[2-9]|[1-9]\d+)\b` and you can't use `[1-9]` instead of `[2-9]` else it will match `1` also. – Pushpesh Kumar Rajwanshi May 09 '19 at 17:12
  • @BlackPearl I read your link. I do not get why it is used in the number case? What do I want to ignore? In the link example, they want to ignore part, and just use it in the matching process. – user9371654 May 09 '19 at 17:14
  • If you do want to get the matched text within the `()` and just need to validate it, you use `?:`. If you need the actual match, you should not use it. – BlackPearl May 09 '19 at 17:14
  • @Pushpesh Kumar Rajwanshi If I want to capture 1 also? just change [2-9] to p[1-9]? – user9371654 May 09 '19 at 17:15
  • @user9371654: If you want to capture one or greater than one then better use just `\b[1-9]\d*\b` – Pushpesh Kumar Rajwanshi May 09 '19 at 17:16
  • Yes, you should use [1-9] if you want to capture numbers greater than 1, and as @PushpeshKumarRajwanshi suggested, you should use `\b` if the number is in the middle of a sentence. – BlackPearl May 09 '19 at 17:16
  • Also, what if the number is greater than 3 digits? – user9371654 May 09 '19 at 17:18
  • `\b[1-9]\d*\b` this will match any number greater than or equal to `1` and hence will match a number greater than three digits like 1234 too. Can you precisely state, what do you really want to achieve? – Pushpesh Kumar Rajwanshi May 09 '19 at 17:20
  • @Pushpesh Kumar Rajwanshi see edit2 – user9371654 May 09 '19 at 17:21
  • @user9371654: Yes my regex above will match all your numbers. [See this demo](https://regex101.com/r/Ht3Bcj/1) – Pushpesh Kumar Rajwanshi May 09 '19 at 17:24
  • Why? This is like asking how can I slice a cake with a chainsaw? Regex is clearly the wrong tool for this job. – Elroy Jetson May 09 '19 at 18:05
  • What else do you suggest? This is part of a longer regex which contains other strings and the number is just part of it. – user9371654 May 09 '19 at 18:11
  • @Pushpesh Kumar Rajwanshi do you mean d* or d+? you have mentioned both in separate comments. Wich one is correct? I think d* – user9371654 May 09 '19 at 18:59

1 Answers1

0

The ?: is to just look for the pattern without capturing. Your case seems to be different. Also, the () are to create a group, again, you do not need that functionality as your use case is quite simple.

If the numbers won't be like 000 (ie no extra initial zeroes), then you can go with ^[1-9]\d*$. If the numbers can come in between the sentences, use \b[1-9]\d*\b to look for word boundaries.

The basic idea is this: you want to match any non-negative number that is not zero. So, the number should not start with a - or 0. So, start with 1-9 (\b[1-9] part of regex), followed by any digits (\d*\b part of regex).

Obviously this leaves out many forms of numbers: floats as decimals (eg 23.456), numbers in exponential form (eg 1.23e4) etc.

Vishesh
  • 16
  • 2