Use of regular Expressions (II.)

Source: Internet
Author: User
Tags ereg expression php and regular expression

Working with instances

After a more comprehensive understanding of regular expressions, let's take a look at how regular expressions are used in perl,php and JavaScript.

In general, the use format of the Perl regular expressions is as follows:

Operator/regular-expression/string-to-replace/modifiers

An operator can be an M or S, representing a matching operation and a substitution operation, respectively.

In which, a regular expression is a pattern that will be matched or replaced, and can consist of any character, meta character, or locator symbol. The replacement string is a string that replaces the found pattern matching object when the S operator is used. The final parameter items are used to control different ways of matching or replacing. For example:

s/geed/good/

The first occurrence of the geed string will be found in the target object and replaced with the good. If we want to perform multiple lookup-substitution operations within the global scope of the target object, we can use the parameter "G", or s/love/lust/g.

In addition, the parameter "I" can be used if we do not need to limit the matching case form. For example

m/jewel/i

The regular expression above will match the Jewel,jewel, or Jewel, in the target object.

In Perl, a specific operator, "=~", is used to specify a matching object for a regular expression. For example:

$flag =~ s/abc/abc/

The above regular expression will replace the string ABC in the variable $flag with ABC.

Next, we add a regular expression to the Perl program to verify the validity of the user email address format. The code is as follows:

#!/usr/bin/perl
# get input
Print "What ' s your email address?\n";
$email = <STDIN>
Chomp ($email);
# Match and display result
if ($email =~/^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + (\.[ A-za-z0-9_-]) +/)
{
Print ("Your email address is correct!\n");
}
Else
{
Print ("Please try again!\n");
}


If you prefer PHP, you can use the Ereg () function to perform pattern matching operations. The Ereg () function is used in the following format:
Ereg (pattern, string)

Where pattern represents the schema of the regular expression, and string is the target object that performs a find-and-replace operation. Also verify the email address, the program code written using PHP is as follows:

<?php
if (Ereg ("^ ([a-za-z0-9_-]) +@" ([a-za-z0-9_-]) + (\.[ A-za-z0-9_-]) + ", $email))
{echo "Your email address is correct!";}
Else
{echo ' please try Again! ';}
?>
Finally, let's look at JavaScript. JavaScript 1.2 has a powerful regexp () object that can be used to match regular expressions. The test () method can verify that the target object contains a match pattern and returns TRUE or false accordingly.

We can use JavaScript to write the following script to verify the validity of the email address entered by the user.

<script language= "Javascript1.2" >
<!--start hiding
function verifyaddress (obj)
{
var email = obj.email.value;
var pattern =/^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + (\.[ A-za-z0-9_-]) +/;
Flag = pattern.test (email);
if (flag)
{
Alert ("Your email address is correct!");
return true;
}
Else
{
Alert ("Please try again!");
return false;
}
}
Stop hiding-->
</script>
<body>
<form onsubmit= "Return to Verifyaddress (this);" >
<input name= "Email" type= "text" >
<input type= "Submit" >
</form>
</body>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.