As the title suggests, I would like to know a regular expression that detects a numeric string with the same number as 10 characters, such as 0000000000
.
I've tried many things using the regular expression checker, but I've also picked up a string of numbers such as 1234512345
, so I'm worried.
I'm sorry for the beginner's question, but I appreciate your cooperation.
regular-expression
There are actually several types of regular expressions that are commonly called regular expressions and are slightly different.In particular, this kind of complexity is different from the behavior we assume that we do not specify the type of regular expression BRE or ERE.The following is a shell script that uses grep
.Inevitably, grep-e
is BRE, and similarly grep-E
is ERE.
Now, let me tell you how to think.
Any [numeric character] is [0-9]
or [[:digit:]]
(Note that [:digit:]
replaces 0-9
in the latter, which should be further bound with []
in practical use.)
[10 or more] is likely to be represented by a repeat match.
US>\{10,\}
for BRE
{10,}
Any [numeric character] is [0-9]
or [[:digit:]]
(Note that [:digit:]
replaces 0-9
in the latter, which should be further bound with []
in practical use.)
[10 or more] is likely to be expressed as a repeat match.
US>\{10,\}
for BRE
{10,}
However, grep-e'[0-9]\{10,\}'test.txt
or grep-E'[0-9]{10,}'test.txt
hits "at least 10 times of any numeric character" as you pointed out.So you need to think about it.
If you use \1
with @metropolis written in the comment, you can pull on the actual hit character.When you use this, you need to use a partial regular expression, so you need to use parentheses
\([:digit:]]\)\1
for BRE
for ERE ([:digit:]])\1
Then it hits 11
or 22
and it doesn't hit 10
or 48
.Once you've done this, you just need to reduce the number of iterations by one, as the comment says
grep-e'\([:digit:]]\)\1\{9,\}'test.txt
for BRE
grep-E' for ERE ([:digit:])])\1 {9,}'test.txt
In this case, the iteration specification is the iteration of the previous partial regular expression \1
(= already hit numeric characters).
grep
if you want grep-o-e...
to show only where it matches, not the entire line.
© 2024 OneMinuteCode. All rights reserved.