vs2013 regex基础匹配:
符号 | 含义 | 范例 |
---|---|---|
. | any sinle charactor | a.o matches “aro” |
* | Match zero or more occurrences of the preceding expression (match as many characters as possible) 貌似只能匹配a或者r,不能像.*可以在中间匹配其他字符 | a*r matches “r” in “rack”, “ar” in “ark”, “aar” in “aardvark” |
+ | Match one or more occurrences of the preceding expression (match as many characters as possible) | e.+e matches “eede” in “feeder” but not “ee”. 用+比用*,可以避免匹配一些莫名其妙的地方 |
.* | Match any character zero or more times (Wildcard *) | c.*e matches “cke” in “racket”, “comme” in “comment”, and “code” in “code” |
\s | 匹配一个空格 | width\s 匹配 "width " |
[0-9] | 匹配指定范围内的所有字符 | 匹配数字 |
[a-z] | 匹配指定范围内的所有字符 | be[n-t] matches “bet” in “between”, “ben” in “beneath”, and “bes” in “beside”, but not “below”. |
[a-z_] | 匹配制定范围内的所有字符 | 匹配“WX_2_QT”中的"WX_"和“_QT” |
^ | Anchor the match string to the beginning of a line or string | ^car matches the word “car” only when it appears at the beginning of a line. |
() | Capture and implicitly number the expression contained within parenthesis | |
[^abc] | Match any character that is not in a given set of characters | be[^n-t] matches “bef” in “before”, “beh” in “behind”, and “bel” in “below”, but not “beneath”. |
{x} | Specify the number of occurrences of the preceding character or group | x(ab){2}x matches “xababx”, and x(ab){2,3}x matches “xababx” and “xabababx” but not “xababababx”. 匹配定长字符串: .{6} 匹配6个字符 |
\r?$ | Anchor the match string to the end of a line | end\r?$ matches “end” only when it appears at the end of a line. |
vs2013 regex函数式、表达式匹配与替换:
匹配目标 | 替换结果 | 匹配表达式 | 替换表达式 | 说明 |
---|---|---|---|---|
m_MainWnd.width = 800; | m_MainWnd.setWidth(800); | width.+\s([0-9]+) | setWidth($1) | .+匹配多个字符 \s匹配单个空格 ([0-9]+) 捕捉了数字部分 用$number进行替换 |
m_StsWnd.y = -1; | m_StsWnd.setY(-1); | y.+\s([0-9-]+) | setY($1) | 进阶:用 ([0-9-]+) 匹配了负数 |
m_iTaraPosSize.x = m_pMetrics->m_TaraWnd.width - 3; | m_iTaraPosSize.setX(m_pMetrics->m_TaraWnd.width - 3); | x.=\s([0-9a-z-._>() ]+); | setX($1) | 进阶:用 [a-z0-9->._() ]+ 来匹配 |
m_iTaraPos[5].x = m_iTaraPos[6].x - SWdispDotX; | m_iTaraPos[5].setX(m_iTaraPos[6].x - SWdispDotX); | x.+=\s([0-9a-z-._>()\[\]+ ]+); | setX($1) | 进阶:用 x.+=\s([0-9a-z-._>()\[\]+ ]+); 多匹配[] |
notepad++ regex基础匹配:
符号 | 含义 | 范例 |
---|---|---|
(…) | <Parentheses mark a subset of the regular expression. The string matched by the contents of the parentheses ( ) can be re-used as a backreference or as part of a replace operation; | |
[^…] | The complement of the characters in the set. For example, [^A-Za-z] means any character except an alphabetic character. | Care should be taken with a complement list, as regular expressions are always multi-line, and hence [^ABC]* will match until the first A,B or C (or a, b or c if match case is off), including any newline characters. To confine the search to a single line, include the newline characters in the exception list, e.g. [^ABC\r\n]. |
+ | This matches 1 or more instances of the previous character, as many as it can. For example, Sa+m matches Sam, Saam, Saaam, and so on. [aeiou]+ matches consecutive strings of vowels. | \s+ matches 1 or more spaces |
* | This matches 0 or more instances of the previous character, as many as it can. For example, Sa*m matches Sm, Sam, Saam, and so on. | For example, Sa*m matches Sm, Sam, Saam, and so on. |
notepad++ regex函数式、表达式匹配与替换:
匹配目标 | 替换结果 | 匹配表达式 | 替换表达式 | 说明 |
---|---|---|---|---|
zero_bmp BITMAP DISCARDABLE “…/…/font/bmpfont/0.bmp” | <file alias=“zero_bmp”>bmpfont/0.bmp</file> | ^([^} \r\n])\s+BITMAP DISCARDABLE ".{11}([^}\r\n])"\r?$ | <file alias="$1">$2</file> | [^…]匹配除了…的字符 |
notepad++ regex替换所有注释
替换空行:
\n\n
替换为
\n
删除空行:
^\s+\n
替换为
空
替换单行的{:
\n{
替换为
{
替换单行/**/:
/*(.*?)*/
替换为
空
替换多行/**/:
选中替换对话框的’matches new line’
/*(.*?)*/
替换为
空
替换//:
1//[^\n]\n
替换为
空
javascript regex函数式:
符号 | 含义 | 范例 |
---|---|---|
\ | 转义(escape) | \. 由于.是特殊字符,所以需要转义 |
/ | 正则表达式的开始和结束 | str.search("/\./") 搜索句号. |
grep perl regex函数式:
匹配目标 | 匹配表达式 | 说明 |
---|---|---|
http://a.gz https://b.gz | https?😕/\K[^ ]+ | s? 匹配0个或多个s,as few as possible |
http://a.gz https://b.gz ftp://c.gz | https?😕/[^ ]+|ftp://[^ ]+ | | 匹配pattern a or pattern b |
\t ↩︎