|
|
split() Function of Regular Expression in Perl
|
Tutorials

Perl

|
Topic |
What is split() Function in Perl?
How to split up a string using regular expression delimiter?
|
|
Explanation |
|
The split() function is used to split up a string using a regular expression delimiter or a character or a string.
Syntax:
split (string);
In the above syntax, split() function takes a "string" as the argument.
Example to split string using regular expression delimiter:
#! C:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
$str1 = "how,are,you";
$str2 = "how!!are!!you";
$str3 = "how1are2you";
@val1 = split(",", $str1);
@val2 = split('!!', $str2);
@val3 = split(/\d+/, $str3);
print "Removed the character Comma","\n";
print "<br>";
foreach $val1 (@val1)
{
print "$val1\n";
}
print "<br>";
print "Removed the string '!!'";
print "<br>";
foreach $val2 (@val2)
{
print "$val2\n";
}
print "<br>";
print "Removed the decimals 1,2";
print "<br>";
foreach $val3 (@val3)
{
print "$val3\n";
}
Result:
Removed the character Comma
how are you
Removed the string '!!'
how are you
Removed the decimals 1,2
how are you
In the above example the string "howareyou" is seperated first with a comma, then by a string "!!", at last
seperated by decimals "1,2", which is removed using a regular expression "/\d+/". All these strings are split and stored
in an array, then using a loop its returned one by one.
|
|
A Note |
|
Simple introduction, basic CGI perl programming codes with examples.
Do send your feedback or suggestions on this tutorial.
This is a copyright content.
|
|
|
|