REGEX: In Linux and More! 
Recently, working on a programming project, I had to do some string pattern matching. From prior experience, I knew "regex" was the perfect candidate. But the problem was, I was unfamiliar with it, and in the past have always relied on internet for help :)

Regular expressions are truly powerful, they are used for pattern matching. Let me try to express how powerful I think they are, search is one of the most basic things that most things are based off...what ever we do, we search (I believe thats why Google's so popular :P - its a basic thing we do, and Google does that very well) - the world is a database containing information. We process this information, and to recall it, we search within our brains (what we already know) and environment (what we don't know). You want to call some one, you lookup their name in a telephone directory; you want to learn about a particular topic in a book, you look though the index for that keyword/pattern; you look at a log file from a program, you use the find function of the editor and search for keywords (like 'error';) etc...You want to download some file from some site, you don't know when its available - you have 2 options, go back to the site regularly and check for it, or automate the process and use a program to download that file - the program finds the file by pattern-matching.

Hopefully, I was able to express how much we do searches (which is based of pattern matching ~ exact match to the search term or approximate match), and how much we rely on pattern matching. This pattern matching can be done in computers using Regular Expressions!!

Getting to the actual point of this post, I decided I will list some useful tips and hints that I found from scouring the internet :P

(This is related to Linux in that, grep is used for searches using regex in Linux)

Sources:
http://www.regular-expressions.info/characters.html

* Regex engines are case sensitive by default, so "cat" won't match "Cat"
* "at" matches "at","cat","atwood" etc
* The following are metacharacters: [,\,^,$,.,|,?,*,+,(,) - this would change based on the engine
* To use any of these characters literally in a string you would have to escape it using a '\', so in order to use * as an actual character rather than a metacharacter in that search, you would use \* instead
* A character following a '\' can possibly have a special meaning, i.e. \d is used to represent a digit between 0 and 9 - meaning you can't 'escape' normal characters like you do for the metacharacters
Here is a list of special sequences:
* \t = tab
* \r = carriage return
* \n = line feed
CHARACTER CLASS
* [ae] matched a or e NOT ae, example gr[ae]y matches grey and gray not graey
* Ranges can be specified using a '-', so [0-9a-fA-F] specifies 1 character (hexadecimal digit, case-insensitive)
* q[^u] matches anything that has a q followed by something not a u

to be continued...

[ add comment ]   |  [ 0 trackbacks ]   |  permalink  |   ( 2.5 / 25 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Next> Last>>