Sunday, May 12, 2013

REGEX for n characters or at least m characters in Unix

{m} Match the prceeding characters exactly m times



Examples Correct Incorrect
A{3} AAA A


AA


AAAA


AAAAA
{m,} Match the prceeding characters atleast m times



Examples Correct Incorrect
A{3,} AAA A

AAAA AA

AAAAA






{min,max} Match the prceeding characters between min and max times



Examples Correct Incorrect
A{3,5} AAA A

AAAA AA

AAAAA AAAAAA


AAAAAAA




Examples :

File Name : numbers.dat

12
12345
123456
12345678
10.1
.1



grep '^[0-9]\{6\} numbers.dat # line should be start between number 0 to 9 and length of the digit is exactly 6

grep '^[0-9]\{4,6\} numbers.dat# line should be start between number 0 to 9 and length of the digit is between 4 and 6

grep '^[0-9]\{3,\} numbers.dat # line should be start between number 0 to 9 and length of the digit is minimum 3

grep '^\.[0-9]' numbers.dat # line should be start with .(dot) and followed by digits


No comments:

Post a Comment

Item - Category Query

      SELECT      msi.segment1 AS Item_Code,       msi.DESCRIPTION AS Item_Desc,       mcs.CATEGORY_SET_NAME,       mck.CONCATENATED_SEGMEN...