The Linux regular expression is similar to the pyhon regular expression.

Source: Internet
Author: User

Use Regular Expressions

You can use regular expressions to do many things. In the following list, you can find some examples of the most common and commonly used regular expressions.

    • ^: indicates that the text string must start with a line. Therefore, if you want to search for rows starting with "hosts", you can run the command: grep-ls '^ hosts'
    • $: indicates the end of a row. Therefore, when the end of the search row is only "hosts", you can use the command: grep-ls 'hosts $ '
    • you can combine ^ and $ in a regular expression to find rows that only contain "yes, the command used is grep-ls '^ Yes $'
    • .: a wildcard that can refer to any character except line breaks. To find rows that contain Tex, tux, tox, or Tix, you can use: grep-ls 't. x'
    • []: indicates that in a regular expression, the characters between square brackets are optional. To find a user named pinda or Linda, run the command grep-ls '[pl] INDA'
    • [^]: Ignore all characters After ^ in square brackets. To search for lines containing text Linda and ignore lines containing only Linda or pinda, run the following command: grep-ls '[^ PL] inda'
    • -: represents a class or a range of characters. This is very useful in commands like tr. The following command converts all lowercase letters into uppercase letters: tr a-Z A-Z
    • \< and \>: Find the start or end mode of a word. The command starting with "San" is grep \ . These regular expressions have two disadvantages: they do not search for rows starting with a specified regular expression, and they are not supported by all tools. However, VI and grep support this usage.
    • \: ensure that characters with special meanings in regular expressions are not interpreted. Run the grep-ls '. host' command to search for a text string starting with any character followed by the text "host '. If you need to find the text string with the dot as the first character followed by "host", you can use the command: grep-ls '\. host'

These regular expressions help you find words that contain specific text strings. You can also use a regular expression to specify the frequency at which the string appears in a word. For example, you can use a regular expression to search for files that exactly contain the user name "Linda. To achieve this goal, you need to use the repeat operator of the regular expression and ensure that the entire regular expression is enclosed in quotation marks. Without quotation marks, you may cause shell to explain your repeated operators.

The list of the most important repeated operators is as follows:

    • *: Indicates that the preceding regular expression may appear once, multiple times, or not at all. Note: Do not confuse it with * in Shell-in shell, * represents any character, while in regular expression, * indicates that the previous regular expression may exist.
    • ? : Indicates that it may be a character (but not a must) at this position ). For example, the command to search for both color and color is grep-ls 'colo. R'
    • +: Indicates that the previous character or regular expression must appear at least once.
    • \ {N \}: indicates that the previous character or regular expression must appear at least N times. When you look for a number between 100 and 999, this is useful: grep-ls '0-9 \ {3 \}'

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.