Using Perl regular expressions to match

Source: Internet
Author: User
Tags regular expression

Perl has long been a popular text-processing tool for its inherent support for regular expressions. In this introductory article, we'll show you how to use regular expressions in your own programs to achieve more powerful text search and substitution capabilities.

We first understand the simplest general expressions: matching. If a matching pattern is found in the string, the matching operation returns the truth value. So the following expression:

$string =~ m/text/

The true value is returned only if the string in the variable "$string" contains the substring "text". This is the most basic regular expression, which matches the characters verbatim. Of course, this is just an attempt to effect a regular expression. Take an example of a four-letter word that needs to be found at the end of "ext." To achieve this, we use a special character ".", a period in a regular expression that tells Perl to match any individual character in it. So the following expression:

$string =~ m/.ext/

Will match the word "text" and "next".

However, this expression is not perfect because it matches part of a longer word that contains "ext", such as "dextrous" and "flextime". We can use the anchor character to limit the location of the match. The "^" character matches the beginning of the string, so:

$string =~ m/^.ext/

Matches "dextrous", but does not match "context".

Similarly, the "$" character matches the end of the string:

$string =~ m/.ext$/

Matches "context", but does not match "dextrous".

If you only want to match the four-letter string at the end of ext, you can combine the two expressions above, like this:

$string =~ m/^.ext$/

Now, what if you need to match a given set of characters instead of any character in the period position? A regular expression provides a method by using square brackets. Take the following expression as an example:

$string =~ m/^[tt]ext$/

This expression matches only the word "text" and "text". A pair of brackets will convert any single character in them. This feature is quite powerful, for example:

$string =~ m/[aeiouaeiou]/

If the $string variable contains a vowel, the example above returns the truth value.

If the first character in parentheses is "^", then it is not an anchor character, but rather a "non" operation that matches any character not in parentheses, so if the $string variable contains only consonants or punctuation, you can adjust the example above to make it return the truth:

$string =~ m/[^aeiouaeiou]/

The square bracket symbol can also specify the range of characters so that you do not have to enumerate a whole string of consecutive numbers or letters, for example, the following example matches any lowercase letter:

$string =~ m/[a-z]/

So far, we've been dealing with one character in a string at a time, but in many cases we need to deal with more complex issues. We use the "|" or segmented operation to achieve this goal. Suppose we want to check whether the $string variable contains "next" or "previous", we can use the following expression:

$string =~ m/next|previous/

If we want to use anchor characters in this expression, then we need to combine the options as if we were using parentheses in arithmetic. So if we want to match only the "next" or "previous" at the beginning of the string, you can write an expression like this:

$string =~ m/^ (next|previous)/

Related Article

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.