Linux regular expression-word, linux Regular Expression
You may have discovered that it is difficult to match a complete word. For example, if you want to match the mode "book", the search will hit the line containing the words "book" and "books", and there will also be the words "bookish", "handbook" and "booky ". Obviously, spaces can be used before and after "book" to limit matching conditions.
□Book □
However, this expression only matches the word "book", and it will lose its plural form "books ". To match singular or plural words, the asterisk metacharacters may be used:
□Books * □
In this way, you can match "book" or "books ". However, if the word is followed by a sentence, comma, hello, or quotation mark, it does not match "book ".
When used together with the wildcard metacharacters (.), it can match zero or multiple times of any character. In the previous example, you can write a complete regular expression as follows:
□Book. * □
This expression matches the string "book", followed by any character or no character, followed by a space. The following are the rows to be matched:
Here are the books that you requested
Yes, it is a good book for children
It is amazing to think that it was called a "harmful book" when
Once you get to the end of the book, you can't believe
(Note that only the second line can match the fixed string "□book □ .) The expression "□book. * □" matches the line containing the words" booky "," bookworm ", and" bookish. Different modifiers can be used to exclude the two matching types. Question mark (?) Is a part of the Extension Set of metacharacters. It matches the previous expression 0 or 1 times. Therefore, the expression:
□Book .? □
It will match "book,", "books," and "booky" instead of "bookish" and "bookworm. To exclude words like "booky", we can use character classes to specify all characters in the position to be matched. Furthermore, because the metacharacter question mark is not available in sed, we must turn to the character class, which will be seen later.
Try to use a regular expression to include everything that is not practical, especially when using grep, sometimes it is best to keep the expression simple and allow omission. However, when using a regular expression to replace sed, you must note that the expressions used should be complete. In the post on this site, we will see a more comprehensive regular expression for searching words.
References: http://www.linuxawk.com/communication/434.html