0

I have some numeric identifiers with a simple, but repeating pattern, that I am searching for within a document. For example, one particular string has five, four digit numbers and a two digit number each separated by a space:

1234 1234 1234 1234 1234 12

I created a regex pattern (used in egrep) that is formatted as follows:

 [0-9]{4}[[:blank:]][0-9]{4}[[:blank:]][0-9]{4}[[:blank:]][0-9]{4}[[:blank:]][0-9]{4}[[:blank:]][0-9]{2}

This works. However, I'm trying to find out if there's a way simplify this. For example, I want to write a regex that says I have five patterns of [0-9]{4}[[:blank:]].

How can I simplify/shorten this regex pattern?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Allan
  • 824
  • 1
  • 8
  • 18
  • 1
    If there is no issue, there is no problem. If you make an attempt to "simplify" the pattern and it stops working, then it would be an ontopic question. – Wiktor Stribiżew Nov 28 '20 at 21:58
  • 1
    Interesting @WiktorStribiżew. As I go through SO for various answers to my questions, there are countless examples where someone answers a question and then in the comments someone else opines something to the effect "Why do X? You can simplify this by doing Y." Are you saying there's no way to shorten this regex and somehow, in my very limited skill set I've somehow found the most efficient way to do this? – Allan Nov 28 '20 at 22:18
  • 1
    This is not an XY problem. This is the case when there is no problem. Yes, you may shorten it using a group and an additional quantifier, but what good is it? Most people would say it would become less readable. And now, SO has its own rules, if you have no issue, this is just off-topic. – Wiktor Stribiżew Nov 28 '20 at 22:38
  • 2
    Any reason you are not using `([0-9]{4}[[:blank:]]){5}[0-9]{2}` or `(\d{4}\s){5}\d{2}` which is "a group and an additional quantifier"? Check out [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean).. it's like a list of whatever you need :) – Scratte Nov 29 '20 at 00:18

1 Answers1

2

You can group any pattern (), and specify the number of repetitions {n}. Here is your example trying with 5 and 6 repetitions:

$ echo '1234 1234 1234 1234 1234 12' | egrep '([0-9]{4}[[:blank:]]){5}'
1234 1234 1234 1234 1234 12
$ echo '1234 1234 1234 1234 1234 12' | egrep '([0-9]{4}[[:blank:]]){6}'
$
Peter Thoeny
  • 3,223
  • 1
  • 6
  • 13