Linux regular expression-word, linux Regular Expression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.