On Regular Expressions

(I originally published this post on my general blog).

Regular expressions are the alternative for full fledged parsers when there is lightweight parsing to do. For example they can be used to match a certain pattern in a file. They are available in all the scripting languages(Perl, Python, Ruby, Tcl, Shell scripting) and tacked on as a library in C++, Java, C#, etc. Here is an example in Ruby that finds floating-point numbers in a file(presumably for more processing with them).

Sample.txt has the following two lines:

#0 0.22 -3 -0.4
#0 5 3 7

Find.rb has the following content:

ary = Array.new
open(’sample.txt’).each do |line|
ary <</[-+]?[09]*\.[09]+/)
end

ary.each{|item| puts item }

Output of the code snippet is the following two lines
0.22
-0.4

You can check out my GPLed Regular Expression Tester here. Pay special attention to the disclaimers listed in the GPL as this little utility has NOT been tested very much.

Here is a language-neutral introduction to regular expressions(it also my reference for the post)

www.regular-expressions.info/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s