0

I'm trying to develop a regex with the following rules:

  • it should accept solely numbers,
  • if the string contains any letters or any other special characters, the whole string should be rejected,
  • regarding spaces, there should only be one consecutive number group, which can be surrounded by spaces,
  • if there are more than one consecutive number group, with spaces in between the groups, that whole string should be rejected.

Example Cases:

  • accepted:
    • 1234
    • [SPACE][SPACE]111[SPACE]
    • [SPACE]111[SPACE][SPACE]
  • declined:
    • 1a234
    • aa1234aa
    • 1234a
    • 12#4
    • [SPACE]11[SPACE]111
    • [SPACE]11[SPACE]111#

So far, I've come up with this ([0-9]+[^\s]*) which can be seen here.

What modifications do I have to do to achieve the scenario I want above?

CinCout
  • 8,291
  • 9
  • 47
  • 55
Can
  • 3,914
  • 6
  • 25
  • 41
  • 1
    [`^\s*[0-9]+\s*$`](https://regex101.com/r/pTDcmH/1)? – Wiktor Stribiżew Dec 10 '19 at 09:51
  • 2
    Try using `^ *[0-9]+ *$` See https://regex101.com/r/d93fCm/1 `\s` could also match newlines. – The fourth bird Dec 10 '19 at 09:51
  • Somehow confusing. *there should only be one consecutive number group* I don't get it. If there is only one number, there is nothing consecutive is there? If it's really just matching a number surrounded by optional space, why so detailed. – bobble bubble Dec 10 '19 at 10:05
  • @WiktorStribiżew No, it's not Swift but I don't get why I have to share the code. This is simply a regex question. Not related to in which language it's implemented in. – Can Dec 10 '19 at 10:14
  • @bobblebubble these are not cases that I've come up with but something I've to comply with – Can Dec 10 '19 at 10:16
  • 3
    There is a difference between `\d` and `[0-9]` in certain regex engines. You may end up matching strings like `۱۲۳۴۵۶۷۸۹` or `୪`. That is why you should always note what regex engine you are going to use with the pattern in question. – Wiktor Stribiżew Dec 10 '19 at 10:18

3 Answers3

3

Use this:

^\s*\d+\s*$

All we need to do is accept one or more digits bounded by zero or more spaces on either side.

EDIT:

Just add a capturing group around the digits to use them later:

^\s*(\d+)\s*$

Demo

CinCout
  • 8,291
  • 9
  • 47
  • 55
  • One thing I forgot to mention. While processing the accepted string later on, I only need the digits. If a digit group gets accepted with spaces by abiding the above rules, I only need the digits. Via regex, is it possible to accept the spaced digits but only retrieve the digits inside at the same time or should do another regex afterwards to retrieve merely the numbers? – Can Dec 10 '19 at 10:23
  • 1
    @Can answer updated – CinCout Dec 10 '19 at 10:24
2

The pattern you tried ([0-9]+[^\s]*) matches 1+ digits and 0+ times a non whitespace character using a negated character class [^\s]* matching any character except a whitespace char (So it would match aa)

It can match multiple times in the same string as there are no anchors asserting the start ^ and the end $ of the string.

If you want to match spaces, instead of matching \s which could also match newlines, you could match a single space and repeat that 0+ times on the left and on the right side.

^ *[0-9]+ *$

Regex demo

If you only need the digits, you could use a capturing group

^ *([0-9]+) *$

Regex demo

The fourth bird
  • 96,715
  • 14
  • 35
  • 52
0
^\s*[0-9]+\s*$

notice that I've used [0-9] instead of \d

[0-9] will accept only Arabic number (Western Arabic Number)

\d may accept all form of digit in unicode like Eastern Arabic Number, Thai,...etc like (١,٢,٣, ๑,๒,๓, ...etc) at least this is the case in XSD regex when its validate XML file.