Regular Expression Basic

1. Basic Regular Expression  

  a. "^"     matching the head of a line. "^" must be the first character in a regular expression ,else it only a common character.

  b. "$"  matching the end of a line. "$" must be the last character in a regular expression ,else it only a common character.

    Attention: The "^" is only an anchor if it is the first character in a regular expression,else it only a common character.

          The "$" is only an anchor if it is the last character in a regular expression,else it also only a common character.

          If you want match a "^" in the begin of a line or "$" in the end of the line ,you can use the special characters with a backslash (\).

      eg:   ^$ , It can match a blank line

    

  c. "."  matching any silger word, except the end-of-line character

  d. "*"  Repeating previous character zero or many times.

      Attention:

         You must remember that modifiers like "*" , "\{3,5\}" ,"\{3,\}" and"\{3\}" only act as modifiers if they follow a common charater set.

      eg:

       *   : Any line contains an asterisk

       \*  : Any line contains an asterisk

         ^*  : Any line starting with an asterisk

  e. [...] Specifying a range of characters

      If you want to mathc specific characters,you can use the square brackets to identify the exact characters you are searching for. The pattern that will match any line of text that contains exactly one numer is.

      Attention: The characters "]‘ and "-" do not have a special meaning if thed directly follow "[".

      eg:

      []      The common character "[]"

      [-0-9]    Any number or "-"

      [^0-9]    Any character other than a number

      [0-9-]    Any number or "-"

      []0-9]    Any number or "]"

      [0-9-z]    Any number, or any character between "9" and "z“

      [0-9\-a\]]   Any number, or "-" or "a" or "]"

      POSIX

      [:alnum:]  Alphanumeric

      [:cntrl:]    Control character

      [:lower:]   Lower case character

      [:space:]   whitespace

      [:alpha:]  Alphabetic

      [:digit:]   Digit

      [:upper:]   Upper case character

      [:blank:]   whitespace, tabs, etc.

2. Extended Regular Expression

a. Repeating previous character minimum and maximum times.

    \{3,5\}  : Repeating previous character 3,4 or 5 times.

    \{3,\}    : Repeating previous character 3 or more times.

    \{3\}   : Repeating previous character only 3 times.

    eg:

       ^A\{2,5\}B      : Any line starting with 2,3,4 or 5 "A"s followed by a "B" 

       ^A\{3,5\}    : Any line starting with 3,4 or 4 "A"

       ^A\{3}\       : Any line starting with 3 "A"

       \{3,5\}     : Any line contains {3,5}

  b.  + ,  Repeating the privouse character at least one times

  c. ?  , Repeating the privouse character zero or one times.

  d. |    It is meaning or between two regular expression.

    eg:

      ( expressin1  | expression2  | expression3 )

3 operator priority

    Regular expression operator priority is following:

      \      

      []

      *, +, ?, \{m\}, \{m,\}, \{m,n\}

      common character

      ^, $

      |  

时间: 2024-10-14 10:48:22

Regular Expression Basic的相关文章

linux 下Regular Expression

正则表达式:Regular Expression,REGEXP元字符:.:表示任意单个字符[]: 匹配指定范围内的任意单个字符[^]: 匹配指定范围外的任单个字符 字符集合: [:digit:], [:lower:],[:upper:],[:punct:] [:alpha:] [:space:],[:alnum:] 字符个数:(贪婪模式)*:匹配其前面的字符任意次 a, b, ab, aab, acb, adb, amnb a*b b ab aab a.*b .*:任意长度的任意字符 \?: 匹

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

Python正则表达式Regular Expression基本用法

资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表达式re模块的基本函数. (1)findall函数的用法 findall(rule,target[,flag])是在目标字符串中找到符合规则的字符串.参数说明:rule表示规则,target表示目标字符串,[,flag]表示的是规则选项.返回的结果是一个列表.若没找到符合的,是一个空列表. 如: 因

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【leetcode】Regular Expression Matching (hard) ★

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

[LeetCode] Regular Expression Matching(递归)

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

Python正则表达式 re(regular expression)

1. 点. .: 代表一个字符 (这个跟linux的正则表达式是不同的,那里.代表的是后面字符的一次或0次出现) 2. 转义 \\ 或者 r'\': 如 r'python\.org' (对.符号的转义) 3. ^ 非或叫做排除 如[^abc]: 任何以非a,b,c的字符 4. | 选择符 如python|perl (从python和perl选择一个) 也可以: p(ython|erl) 5. ? 可选项 如: r'(http://)?(www\.)?python\.org' (http://和w

[LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

LeetCode10 Regular Expression Matching

题意: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype shoul