Posts Tagged ‘正则表达式’

用SQL语句筛选域名

星期五, 2月 20th, 2009

域名的主体部分是由数字(0-9)字母(a-z)以及符号”-”组成的,其中符号“-”不能在开头或结尾,也就是说域名至少有一个字母或数字。

在每天掉下来的几万个域名中筛选想要的域名,一个基本的方法就是区分这些数字和域名,用mysql的正则表达式可以这么写:

1.不包含“-”的:where `name` not like ‘%-%’

即名称中不包含“-”符号

2.不包含数字的:where `name` not regexp ‘[^a-z-]‘

即名称中只包含字母和“-”符号

3.不包含字母的:where `name` not regexp ‘[^0-9-]‘

即名称中只包含数字和“-”符号

4.只包含字母的:where `name` regexp ‘^[[:alpha:]]+$’

即整串都是字母组成

5.只包含数字的:where `name` regexp ‘^[[:digit:]]+$’

即整串都是数字组成

程序中为了书写方便,也可以直接用AND用1/2/3组合出4/5来。

ActionScript 3.0 Regular Expressions

星期一, 9月 15th, 2008

因为开始看Flex3和ActionScript3的文档,边看边把相关的有用内容笔记一下好了。

先说说正则表达式。

英文版的可以看Adobe的这个页面:Using Regular Expressions

ActionScript 3.0的正则表达式,是遵循ECMA-262的规范的。

1.AS3的正则表达式使用“/”符号作为开始和结束。

比如要匹配“ABCD”的字符串,就使用/ABCD/

2.AS3使用“\”符号作为转义符,相关的转义内容包括:

  • \b 匹配单词与非单词的边界,
  • \B 匹配所有非\b的情况,
  • \d 匹配任一个数字,
  • \D 匹配数字外的所有符号,
  • \f 匹配换页符号,
  • \n 匹配换行符,
  • \r 匹配回车符,
  • \s 匹配空白字符,如空格,制表符,回车,换行,
  • \S 匹配所有非空白字符,
  • \t 匹配制表符,
  • \unnnn 匹配使用nnnn编码的unicode字符,
  • \v 匹配垂直进纸符,
  • \w 匹配所有字母符号数字符号和“_”,
  • \W 匹配所有的非\w字符,
  • \xnn 匹配所有的以nn编码的Ascii字符

3.特殊字符

^    匹配一行的开头

$    匹配一行的结束(与\n类似)

.    匹配除了换行符外的任一字符,当s标志打开的时候,可匹配换行符

*    匹配之前的表达式0次或更多

+    匹配之前的表达式1次或更多

?    匹配之前的表达式0次或1次

括号 ( 和 )     定义表达式组,即括号中为一个或多个完整的表达式,用|分隔

中括号 [ 和 ]    定义表达式类,即中括号内所定义的字符范围内的任意可能的一个字符

|    或关系,匹配符号前一个表达式或符号后一个表达式

大括号 { 和 }    匹配次数,{n}:匹配n次; {n,}:匹配>=n次;{m,n}:匹配m~n次

UltraEdit里的正则表达式

星期四, 9月 4th, 2008

格式跟UNIX/Perl略有不同吧,以下是UE帮助里面的

Symbol Function
Matches the start of line - Indicates the search string must be at the beginning of a line but does not include any line terminator characters in the resulting string selected.

(用%匹配一行的开始)
Matches the end of line - Indicates the search string must be at the end of line but does not include any line terminator characters in the resulting string selected.

(用$匹配一行的结束)
Matches any single character except newline

(用?匹配除了新行外的任意单一字符)
Matches any number of occurrences of any character except newline

(*匹配除新行外的任意数目的任意字符)
Matches one or more of the preceding character/expression.  At least one occurrence of the character must be found.

(+匹配除新行外的至少1个的指定字符)
++ Matches the preceding character/expression zero or more times.

(++匹配指定字符的0或多次,就是除1次外的)
^b Matches a page break

(用^b匹配一个分页符)
^p Matches a newline (CR/LF) (paragraph) (DOS Files)

(用^p匹配dos格式下的一个新行符)
^r Matches a newline (CR Only) (paragraph) (MAC Files)

(用^r匹配MAC 格式下的一个新行符)
^n Matches a newline (LF Only) (paragraph) (UNIX Files)

(用^n匹配UNIX格式下的一个新行符)
^t Matches a tab character

(用^t匹配制表符)
[ ] Matches any single character, or range in the brackets

(匹配中括号内的任一字符)
^{A^}^{B^} Matches expression A OR B

(匹配表达式A或表达式B)
Overrides the following regular expression character

(忽略后面的正则表达式)
^(…^) Brackets or tags an expression to use in the replace command.  A regular expression may have up to 9 tagged expressions, numbered according to their order in the regular expression.The corresponding replacement expression is ^x, for x in the range 1-9.  Example: If ^(h*o^) ^(f*s^) matches “hello folks”, ^2 ^1 would replace it with “folks hello”.

^(A^) 将表达式A匹配到的内容赋予^1,在替换过程中可以使用^1进行动态替换。

如果有更多的如:^(A^)^(B^)^(C^)^(D^),若匹配成功,那么替换内容中可以分别使用^1^2^3^4提取ABCD所匹配到的内容。