Regular Expressions, part 3

Part 1 gives a general overview of the meaning of operators.

Part 2 shows basic examples of the regular expressions concept.

Okay, that was simple enough. Now lets try something trickier. Here is a very powerful aspect of string matching. This takes all the characters between the <title> and </title> tags and places it into the variable $1.

if ($string =~ /<title>(.*)<\/title>/i){}

So now an if statement becomes a dynamic part of a program! The period says to look for a single occurrence of any character after the title widget and the * continues until the ending of the match the /title widget.
Now here's a simple set of code with a great regexp. The code iterates over a hash and then at the third line does a substitution:
for (sort keys %tabhash){
my($name) = $_;
$name =~ s/^(.)/\u$1/;           
<a href="/$_.html?orderno=$FORM{'orderno'}">$name</a><br>
What is that doing?
It substitutes the first character of a string (the ^(.) passes the first character of the string into the variable $1) with the uppercase letter (that's what the \u$1 does--forces $1 to be uppercase).
This changes the word furniture into the word Furniture.

Have a date you want to split into three fields? You could call split with an array:

@datea = split(/\/,$date); 
if  ($date =~ /(..)\/(..)\/(..)/) { $month = $1; $day = $2; $year = $3; } 

Return to Perl Help

[Our portfolio] [A history of Computers and Networks]
[Perl help] [ABAP help] [MySQL help]
[TCP/IP troubleshooting] [HTML help] [Feedback] [Humor]
Advertise on Golden Ink's Georgia Network

In Association with