Linux Regular Expressions-the span of characters

Source: Internet
Author: User

Metacharacters allow you to specify repeated occurrences of the character. Consider the following expression:
11*0
It will match each of the following lines:
10
110
111110
1111111111111111111111111110
These meta-characters make the regular expression scalable.

Now let's look at a couple of metacharacters that specify the span and determine the length of the span. You can specify the minimum or maximum number of occurrences of a letter or regular expression.

Use \{and \} in grep and sed. AWK does not support. In any case, curly braces surround one or two parameters.
\{n,m\}

N and m are integers between 0 and 255. If you specify only \{n\} itself, it will exactly match the preceding character or n occurrences of the regular expression. If \{n,m\} is specified, then the number of occurrences is any number between N and M.

For example, the following expression matches "1001", "10001" and "100001", but does not match "101" or "1000001":
10\{2,4\}1

This pair of metacharacters is useful for matching data in fixed-length fields, which can be obtained from the database and used to match formatted data such as phone numbers, U.S. Social Security numbers, inventory part IDs, and so on. For example, the social Security number is formatted as: 3 digits, a hyphen, followed by 2 digits, a hyphen, and then 4 digits. Can be described as a pattern:
[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}

Similarly, the North American phone number can be described using the following regular expression:
[0-9]\{3\}-[0-9]\{4\}

If you use awk, the curly braces are not available, and you can simply repeat the appropriate number of character classes:
[0-9] [0-9] [0-9]-[0-9][0-9][0-9][0-9]


Reference: http://www.linuxawk.com/communication/451.html

Linux Regular Expressions-the span of characters

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.