Home > OS >  About the grep number of precise matching of regular expressions, ~ I got talent show
About the grep number of precise matching of regular expressions, ~ I got talent show

Time:09-27

1. Numbers in the file. Search TXT contains six consecutive Numbers, followed by the character ". "all the rows

 
[liuqiang @ mu01 grepTest] $cat Numbers. TXT

123214.
0987654 456789.678 9 192
1234567.
222111. 21.
[liuqiang @ mu01 grepTest] $grep '[0-9] \ \ {6} \' Numbers. TXT//method 1
123214.
0987654 456789.678 9 192
1234567.
222111. 21.
[liuqiang @ mu01 grepTest] $grep '^ \ [0-9] {6 \} \.' Numbers. TXT/method/2
123214.
222111. 21.
\ [liuqiang @ mu01 grepTest] $grep '& lt; 6 \ \ [0-9] {} \. 'Numbers. TXT/method/3
123214.
0987654 456789.678 9 192
222111. 21.
\ [liuqiang @ mu01 grepTest] $grep '& lt; 6 \ \ [0-9] {} \. \ & gt; 'Numbers. TXT/method/4
[liuqiang @ mu01 grepTest] $


Above, ask a few questions:
2 ^ 1. The method of [XXX] this form, refers to the added ^, it must meet the full match, that is just the 6 Numbers do string, so 456789.678 this line is no good,
2. The method using a string defined in 3 ways, did not consider the ending, so 456789.678 is selected,
3. The traditional way is to method 1, but the method 1 simply consider included, so 1234567. It is seven digits, has been selected,
4. Why method 4 execution result is empty?
The above four issues, the front three don't know if I understand right, the fourth I still don't understand, please the great god give directions,

Thank you very much!



CodePudding user response:

Method 2 in the "^" what is the meaning of the beginning, your ^ followed is [0-9] so will match only to begin with, then look under one condition, matching [0-9] {6} at least six times, so in addition to the first line and last line meet the conditions, the rest were not satisfy
Does not meet the reason:
The second line meet begin with Numbers, but not satisfy matching six times, so ignore,
The third line meet begin with Numbers, but not satisfy matching six times, so ignore,
2. The second line is matched to 456789.678 is that you use a rivet lines "\ & lt;" Line, but no rivets, so all match,

I also not sure, above all is personal opinion,

CodePudding user response:

^ 1, is the beginning of a line locator, add a said to six figures. At the beginning of
2, 3, you match is add a word is the first of six figures., whatever is there, behind the word so that a few matches
3, you don't have to add other qualification, grep will order according to the character sequence matching, such as method 1, 1234567. It starts at 1 first, find the previous six Numbers can match, but the seventh character is not the point, then start from 2 match, found that accords with a condition, so they match the
4, method 4, I also don't understand, you add the word first positioning in line with the suffix locator, by rights should be a continuous characters for a word, but it seemed to point out the words, may I send the wrong, this isn't quite understand.
  • Related