Archive for the Category » language «

A colleague is looking to learnregexso 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 ourRegular Expressionsto learn other topics relevant to the Internet while simltaneously expanding on regex knowledge.

Regex Quick Start

Avertical bar |, sometimes known as a pipe, is an “OR” 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 the “capital”, or “shifted version”, of the backslash key.

Parentheses are round brackets () that select a section of a regular expression. They can be used similarly to in mathematics when theorder of operationsis adjusted. The “selection” can be referenced elsewhere though we’ll cover that later.

Adot .is a special character which matchesanycharacter other than “newline” characters.

A backslash \ is sometimes called anescapecharacter. There are many characters, such as the parentheses and the vertical bar above, that have special meanings. The backslash is used to take away (escape) those special meanings. उदाहरणार्थ, to specify an actual full stop, you need to escape the special dot character: \.

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 a “wildcard” pattern, which matches a string of any length: .*

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

Maybe you have more that you still want to match? उदाहरणार्थ, if I puthttp://swiftspirit.co.za/downloads/and I still want the pattern to match that or any other url under my web site. Or maybe its okay for http://anything.google.com/anythingto also match. We can use the special dot and star characters from rules 3 आणि 5 from above:

http://((|dogma\.)swiftspirit\.co\.za|(|.*\.)google\.com)/.*

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 google.com pattern. The vertical bar indicates two options here: either blank (so we end up with just google.com), or.*\. (so we haveanythingfollowed by a literal dot and that is then followed by google.com).

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 indirect “work”, demonstrating how to flush DNS caches in a variety of systems.

(20:19:25) A: was playing WoW on Saturday(20:19:40) B: yea dude(20:19:44) A: They have chat channels similar to IRC, though its specific to the area you’re in(20:19:52) B: lol(20:20:01) B: thats kewl(20:20:05) A: a guy put in “I can like to be wearing… “(20:20:23) A: so I replied “a jean pant”(20:20:28) B: lol(20:20:50) A: and he replied “of ‘n rokkie”(20:21:00) A: I fell off my chair