Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





Regular Expression Operator m// in Perl


Tutorials Perl

Topic

What is Regular Expression Operator m// in Perl?



Explanation
m// Operator:

The "m//" supports so many factors,"s" option treats the string being searched as if its a single line,"m" option allows matching of individual lines in a multi line string.If the option "I" is used it matches the string in a case insensitive manner.Another important option is "g" which will have a global scope, it also initialises a pointer to the begining of the string.For every match the pointer moves till the end of the substring, if no match found it will be at the starting point.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string = 'average';
    if ($string =~ m/^A/i)
     {
      print "Matched A with a since 'i' option is used.";
     }  
Result:
    Matched A with a since 'i' option is used.

In the above example, we used the "i" option, so "a" is matched with "A" to display a result.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string = 'average\nage';
    if ($string =~ m/^a/m)
     {
      print "Matched 'a' in two different lines.";
     }  
Result:
    Matched 'a' in two different lines in a string.

In the above example, we used 'm' option so that the 'a' is matched in two lines as both lines have 'a' at the begining of the string

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string = 'Fax: 8000-8688';
    while ($string =~ m/(\d{4})/g) 
     {
      print "'$1' found at position ".(pos($string)-length($1))."<br>";
     }
Result:
    '8000' found at position 5
    '8688' found at position 10

In the above example we have used the 'g' option with the 'm//g' operator, so that using the pointer we can find the position of the strings by negating the current position of the pointer from the length of the string.






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.

Other Links

web hosting