0

[0-9] means to include a single digit. (123) means to include the numbers 123. (0123456789) means to include all those numbers in that order. But (0-9) is not a valid expression right? Are ranges allowed inside of ranges?

Allan
  • 11,170
  • 3
  • 22
  • 43
Av1541
  • 47
  • 3
  • 4
    `(0-9)` means a capturing group with 3 characters literally: zero, minus, nine. – zerkms Feb 09 '18 at 03:25
  • *"But (0-9) is not a valid expression right?"* - No it's a perfectly valid regex. `(...)` is a capturing group and not a range. You can always just plug it into an online regex such as https://regex101.com/ to find this out yourself. – Spencer Wieczorek Feb 09 '18 at 03:26
  • 1
    All three answers have been downvoted. Perhaps the downvoter(s?) would care to post an answer. – Keith Thompson Feb 09 '18 at 04:50
  • Yeah could you add some explanations about what we did wrong? thanks – Allan Feb 09 '18 at 05:15
  • Possible duplicate of [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Biffen Feb 09 '18 at 07:56

3 Answers3

1

(0-9) would match the string '0-9' literally. The hyphen/minus sign is used inside character sets (things like [a-z]) only. Therefore, it is completely valid but does not do what you think it does.

T Tse
  • 680
  • 5
  • 18
1
  • [0-9] will match one single digit character
  • 123 is the regex to match the chain 123
  • 0123456789 is the regex to match the chain 0123456789
  • 0-9 will match the string 0-9 literally

Now, if you use parenthesis, you are using capturing group that you could refer later in your regex or in your replacement string by \1 \2 \3 etc for respectively the 1st, 2nd and 3rd capturing group.

You could imagine a regex that matches a string if 2 identical characters are used in the it:

(.).*\1

DEMO: https://regex101.com/r/nFwxRo/1

You have many possible usages of backreferences and capturing groups so I believe they are a fundamental concept to learn if you want to use regex

For more info on regex:

http://www.rexegg.com/regex-quickstart.html

To test your regex:

https://regex101.com/

Regex are cool but might be a bit difficult at first glance! Good luck

Allan
  • 11,170
  • 3
  • 22
  • 43
1

There are different kinds of regular expressions. (0-9) is valid either as a basic regular expression or as an extended regular expression, with different meanings. In neither case is the 0-9 portion treated as a range.

In a basic regular expression (for example, the kind recognized by the grep command), (0-9) simply matches that sequence of 5 characters: '(', '0', '-', '9', ')'.

In an extended regular expression (for example, the kind recognize by egrep or grep -E), (0-9) matches the 3-character sequence '0', '-', '9', with the parentheses indicating grouping (and possibly, depending on the context, capturing the contained group for use elsewhere).

https://www.regular-expressions.info/posix.html

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578