Regex in Coding

"In computing, a regular expression (abbreviated regex or regexp) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace" -like operations. The concept arose in the 1950s, when the American mathematician Stephen Kleene formalized the description of a regular language, and came into common use with the Unix text processing utilities ed, an editor, and grep (global regular expression print), a filter." - Wikipedia.

In a Webapp regex usually used to validate a form, to see if the input is valid or not and give a notice if there is an error in the form. Almost all programming language use Regex, e.g: Javascript, PHP, Ruby, etc. There is two things we should know in Regex, pattern and modifiers.

 

Pattern

 

Pattern is regex syntax which will compare if the text contain characters we need or not. See the table below for quick reference :


  • Any character except newline. \.

  • A period (and so on for \*, \(, \\, etc.) ^

  • The start of the string. $

  • The end of the string. \d,\w,\s

  • A digit, word character [A-Za-z0-9_], or whitespace. \D,\W,\S

  • Anything except a digit, word character, or whitespace. [abc]

  • Character a, b, or c. [a-z]

  • a through z. [^abc]

  • Any character except a, b, or c. aa|bb

  • Either aa or bb. ?

  • Zero or one of the preceding element. *

  • Zero or more of the preceding element. +

  • One or more of the preceding element. {n}

  • Exactly n of the preceding element. {n,}

  • n or more of the preceding element. {m,n}

  • Between m and n of the preceding element. ??,*?,+?,

  • Same as above, but as few as possible. {n}?, etc.

  • Same as above, but as few as possible. (expr)

  • Capture expr for use with \1, etc. (?:expr)

  • Non-capturing group. (?=expr)

  • Followed by expr. (?!expr)

  • Not followed by expr.

 

Modifiers

 

Modifiers is regex options just like if we want make find process with case sensitive characters. See the table below for quick reference :


  • Global /g

    Whether to test the regular expression against all possible matche in a string, or only against the first.

  • IgnoreCase /i

    Whether to ignore case while attempting a match in a string.

  • Multiline /m

    Whether or not to search in strings across multiple lines.


To make it easier to understand, let's move to implementations section.

 

Implementations

 

1.Javascript

 

		[code language="javascript"] 
			var patt=new RegExp(pattern, modifiers);
				//or more simply var patt=/pattern/modifiers; 
		[/code]

As you can see above, we can simply declared a regex pattern with / (slash) character. And how to use it?


	[code language="javascript"] 
		//with exec() => return the matched text or null patt.exec(string)
		//with test() => return true or false patt.test(string) 
		//with compile() => compile || change and recompile a regex patt.compile(patt)
		//with replace() => replace the text patt.replace(pattern, word) 
	[/code]

 

2.Rails

 

		[code language="ruby"]
			 # simple with ( string =~ pattern ) 
			 # or oop style pattern.match(string)
			 # or use gsub to replace the text string.gsub(pattern, replacement)
	 	[/code]

 

To train your skill in regex, I do recommend you to use regexpal.com. Happy Code...

 


Ruby On Rails Blog and Web Development | 41studio

By Admin on October 03, 2013