2

I am simply looking how to detect and odd or even amount (quantifier), or a certain amount (not a range) of a character in a text file. For example this regular expression detects an odd digit:

[1,3,5,7,9]

This regular expression detects an amount (1 through 9) of digits:

\d{1,9}

I would like to find an odd amount of digits.

\d{1|3|5|7|9}

However this syntax is not the right one. Could anyone give me the right answer?

Galaxy
  • 2,363
  • 2
  • 25
  • 59

2 Answers2

3

You could use grouping with a word boundary to find one digit followed by any number of two consecutive digits:

\b\d(\d\d)*\b

Here's an example: http://rubular.com/r/vLTJSJIOJh

Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • 1
    You might wanna convert it to non-capturing group [`\b\d(?:\d\d)*\b`](https://regex101.com/r/eU8rK8/2). – AKS May 18 '16 at 06:07
1

you can use lookahead and lookbehind and 2n+1 pattern

(?<!\d)(?:\d\d)*\d(?!\d)

(?<!REGEX) match negative ahead, (?!REGEX) match negative behind, (?:) make it non capture.

tests: http://rubular.com/r/RhTtKq9m9O


  1. Regex lookahead, lookbehind and atomic groups
Community
  • 1
  • 1
delta
  • 3,778
  • 15
  • 22