Archive for the Category » 语 «

星期一, 4月6日, 2009 | 笔者:

A colleague is looking to learn 正则表达式 so I decided to put it up here. This is the first in a series of regex-related articles. In the parts to follow, we’re going to be using our Regular Expressions to learn other topics relevant to the Internet while simltaneously expanding on regex knowledge.

Regex Quick Start

  1. 一个 竖线 |, sometimes known as a pipe, is an “或” operator in regular expressions. It indicates that the pattern to the left OR to the right is acceptable. On the keyboard, this key is typically thecapital”, 或 “shifted version”, of the backslash key.
  2. Parentheses are round brackets () that select a section of a regular expression. They can be used similarly to in mathematics when the order of operations is adjusted. “ “selectioncan be referenced elsewhere though we’ll cover that later.
  3. 一个 dot . is a special character which matches any character other thannewlinecharacters.
  4. A backslash \ is sometimes called an 逃生 character. There are many characters, such as the parentheses and the vertical bar above, that have special meanings. The backslash is used to take away (逃生) those special meanings. 例如, to specify an actual full stop, you need to escape the special dot character: \.
  5. A star * is an operator which indicates that the preceding character can be repeated zero or more times. This is often combined with the dot to produce awildcardpattern, which matches a string of any length: .*

Using the vertical bar and the escape character

Here we have a number of example web addresses:


If I want a pattern that matches them all, I could use the vertical bar (rule 1 above) to separate them. I’d then also need to use the escape character (rule 4 above) for the full stops:


To above will match every URI in my example however it isn’t specifically efficient or elegant.

Using parenthesis

The same can be achieved with the following, shorter, 正则表达式, utilising the parenthesis from rule 2 above:


Notice that I’ve simply placed common items, 例如 “HTTP://” 和 “/”, outside the selections.

What if you want to match text that might be anything?

Maybe you have more that you still want to match? 例如, if I put and I still want the pattern to match that or any other url under my web site. Or maybe its okay for to also match. We can use the special dot and star characters from rules 3 和 5 from above:


Simply adding that any character can appear zero or more times lets a lot more match without adding too much to the regex’s complexity:


The above is from in front of the pattern. The vertical bar indicates two options here: either blank (so we end up with just, 或 .*\. (so we have anything followed by a literal dot and that is then followed by

Practise makes perfect

We can already see from the last example that a regular expression can very quickly become complex. Much like any language, being easily able to read a regular expression takes practice. If you have a relevant need to work with regular expressions, you’re in for a treat.

部分 2 should be due in a few days. I’ll be doing some very indirectwork”, demonstrating how to flush DNS caches in a variety of systems.

星期二, 1月13日, 2009 | 笔者:

(20:19:25) 一个: 在星期六玩魔兽
(20:19:40) 乙: 是的
(20:19:44) 一个: 他们有类似于IRC的聊天频道, 尽管它特定于您所在的地区
(20:19:52) 乙: 大声笑
(20:20:01) 乙: 多数民众赞成
(20:20:05) 一个: 一个人放进去 “我喜欢穿… “
(20:20:23) 一个: 所以我回答 “牛仔长裤”
(20:20:28) 乙: 大声笑
(20:20:50) 一个: 他回答 “或一件衣服”
(20:21:00) 一个: 我从椅子上摔下来了