Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex and grep match only string with only single or double digit

Tags:

regex

grep

I need to extract a string with only single or double digit number in them. my file (test) looks like

test1correct
test12something
test123wrong

In the above example, i want to grep only for test1correct and test12something

I tried this grep "test[0-9]{1,2}" test but it gives me all the 3 lines.

like image 622
user238021 Avatar asked Jul 31 '11 15:07

user238021


People also ask

Which regex matches one or more digits?

+: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.

How do you grep a single digit?

The regex \b[0-9]\b will match a single digit surrounded by word boundary character, the -o option is used to print only that result and not the whole line as the default behavior does. This will output all the numbers composed of a single digit.

Which grep command will display the number which has 4 or more digits?

[0-9] matches any digit (like [[:digit:]] , or \d in Perl regular expressions) and {4} means "four times." So [0-9]{4} matches a four-digit sequence. [^0-9] matches characters not in the range of 0 through 9 .

Does grep work with regex?

The grep command (short for Global Regular Expressions Print) is a powerful text processing tool for searching through files and directories. When grep is combined with regex (regular expressions), advanced searching and output filtering become simple.

What is the regex for finding a single digit?

\d for single or multiple digit numbers To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.


2 Answers

use: grep "test[0-9]{1,2}[^0-9]"

like image 79
Kaken Bok Avatar answered Oct 21 '22 03:10

Kaken Bok


Using lookaheads and lookbehinds you can specify "exactly one digit" or "exactly three digits" or whatever. This does exactly one digit:

echo 'WB123_4' | grep -Po '(?<![[:digit:]])([[:digit:]]{1})(?![[:digit:]])'
Result: 4

What it is doing is, find a digit that is not preceded by a digit, and also not followed by a digit. Also works for more than one digit. This does three digits, then at least one of anything else, then one digit:

echo 'WB123_4' | grep -Po '(?<![[:digit:]])([[:digit:]]{3})(?![[:digit:]]).+(?<![[:digit:]])([[:digit:]]{1})(?![[:digit:]])'
Result: 123_4

While I'm at it, this combination of grep and sed will find a string with three digits, then one or more of anything else, then one digit, and extract just those parts nicely. (There might have been another way to do that just in grep with groups.)

echo 'WB123_4' | grep -Po '(?<![[:digit:]])([[:digit:]]{3})(?![[:digit:]]).+(?<![[:digit:]])([[:digit:]]{1})(?![[:digit:]])' | sed -r -e 's/[^[:digit:]]+/ /'
Result: 123 4

Note: the -P flag to grep means to use Perl-style regular expressions, which lets you use lookaheads and lookbehinds.

like image 21
David M. Perlman Avatar answered Oct 21 '22 03:10

David M. Perlman