Tuesday, November 26, 2013

Week 11 of Coding

Assignment 2

This in fact was not a really hard assignment, it only has very straightforward for loops and recursions. So I went on the web and looked over more background information about regular expressions.

Firstly, this complex syntax is used to check whether a given string matches or contains a given pattern. But it is not this simple, it has a flexible system that allows check upon a pattern not just fixed characters. Like in the assignment, we could match a symbol to multiple number of spaces.

It consists of various symbols and patterns,
a, X, 9
ordinary characters just match themselves exactly. 

. ^ $ * + ? { [ ]  | ( ) 
meta-characters with special meanings (see below)
 
. (a period)
matches any single character except newline 'n'

w    
matches a "word" character: a letter or digit or underbar [a-zA-Z0-9_]. 
It only matches a single character not a whole word.

W 
matches any non-word character.

w+
matches one or more words / characters

b 
boundary between word and non-word

s 
matches a single whitespace character, space, newline, return, tab, form

S 
matches any non-whitespace character.

t, n, r 
tab, newline, return

D
matches anything but a digit

d 
matches a decimal digit [0-9]

d{1,5}
matches a digit between 1 and 5 in lengths. 

{n} d{5}
matches for 5 digits in a row

^
match the start of the string

$
match the of the string end

*
matches 0 or more repetitions

?
matches 0 or 1 characters of whatever precedes it

 
use . to match a period or  to match a slash. 
With these 'rules' I believe they can compare any complex syntax in an efficient way.

The regular expressions also have some methods such as search, sub, compile.

Search is just for searching a regex pattern.

sub replaces occurences of certain sub-strings.

compile in my understanding is putting patterns together to build an object that is necessary for searching or matching.

No comments:

Post a Comment