Writing More Efficient Programs Using Regular ExpressionsWho is this for? Pre-requisite skills you need to have Introduction Simple Example Check the string "Concept Solutions Corporation" and see if it has the string "on" in it. Unless the language has a pattern matching function, you have to write a pattern matching function yourself. You will have to get the length of the string, go through the string one byte at a time and match the string "on" against each byte. If you want to take the last string that matched, you need a routine to 'remember' that it has found a first match. To make our life easier, we'll just assume that we want to see if the string has the pattern "on". You can write a routine to do this: $str = "Concept Solutions Corporation"; foreach (1..length($str)-2) { if (substr($str, $_, 2) eq "on") { print "String found/n"; last; } } Note that this is just a simple compare. If you want to return the string before the pattern, you will need to add more code. This whole routine can be written this way: $str = "Concept Solutions Corporation"; Yes, that's right, you have just reduced 7 lines of code to just 2. Talk about efficiency - and that is just the start. Details, Details Metacharacters are used to define the pattern. Qualifiers are used to limit the number of times the pattern is searched. So in our example pattern "on", the metacharacters are o
The copyright of the article Writing More Efficient Programs Using Regular Expressions in Perl is owned by Philip Yuson. Permission to republish Writing More Efficient Programs Using Regular Expressions in print or online must be granted by the author in writing.
Go To Page: 1 2 Articles in this Topic Discussions in this Topic |