Python Reptile practice - Regular Expressions (1) regular expression syntax

Reproduced here are summarized from rookie tutorial.

RE One of the most commonly used functions, submit the form. E.g:

When the user registration form, allowing only the user name contains characters, numbers, and the underscore character connection (-), and set the length of the user name is 3-15 characters

 Template matching is ^ [az 0-9 _-] {3,15} $

In fact, regular expression has three roles:

1. Data validation patterns within the test string match.

2. Replace text, you can use regular expressions to identify specific text in the document, completely delete the text or replace it with other text.

3. extracted based on pattern matching substring from a string to find a specific document or within a text input field.

grammar:

Method of constructing a regular expression and a way to create a mathematical expression of the same. That is, using a variety of metacharacters and operators can combine small expressions together to create larger expression. Component Regular expressions can be a single character, character set, the range of characters between the selected characters, or any combination of all these components.

Regular expression pattern by a common text characters (e.g. characters a to z) and special characters (referred to as "meta character") thereof.

Modes are described in the text search to match one or more strings. Regular expression as a template, a character pattern to match with the search string.

  character:

  1. ordinary characters

    Common character including not explicitly designated as metacharacters all printable and non-printable characters. This includes all uppercase and lowercase letters, all numbers, all punctuation and other symbols.

  2. metacharacters

    (1) non-printing characters.

      \ F Matches a form feed character

      \ N Matches a newline

      \ R match a carriage return

      \ T matches a tab

      \ V matches a vertical tab

      \ S Matches any whitespace characters, including spaces, tabs, page breaks, and so on. == \ f \ n \ r \ t \ v

      \ S Matches any non-whitespace characters. == [^ \ f \ n \ r \ t \ v]

    (2) special characters. (Matching special characters need to add \ escape)

      $ Matches the input end of the string.

      () Marked the first sub-expression

      * Matches the preceding subexpression zero or more times. == {0}

      + Matches the preceding subexpression one or more times. == {1}

      . In addition to matching newline \ any single character other than n.

      [Marks the start of expression in parentheses.

      ? Matches the preceding subexpression zero or one, or specify a non-greedy qualifiers. == {0,1}

      \ The next character is marked as or special characters, or literals, or back-reference, or an octal escape.

      ^ Matches the beginning of the string, the non-use logic expression in square brackets.

      {Tag qualifiers start expression.

      | A choice between two specified. Or logic.

    (3) qualifier, non-greedy greedy match (minimum) Match

      * Matches the preceding subexpression zero or more times. == {0}

      + Matches the preceding subexpression one or more times. == {1}

      ? Matches the preceding subexpression zero or one, or specify a non-greedy qualifiers. == {0,1}

      {N} n is a non-negative integer. Matching the determined n times.

      {N,} n is a non-negative integer. Matching at least n times.

      {N, m} m and n are non-negative integers, where n <= m. Match at least n times and match up to m times.

Guess you like

Origin www.cnblogs.com/liuchaodada/p/12070500.html