Regular expressions-character class subtraction

Source: Internet
Author: User
Tags character classes

character class Subtraction: [Base_group-[Excluded_group]]a character class defines a set of characters. character class subtraction produces a set of characters that are the result of excluding characters from another character class from one character class.

Character-class subtraction expressions have the following form:

[ Base_group-[ excluded_group ]]

Square brackets ([]) and hyphens (-) are mandatory. Base_group is a positive or negative character group. The excluded_group section is another positive or negative character group, or another character class subtraction expression (that is, you can nest character class subtraction expressions).

For example, suppose you have a basic group that consists of characters from "a" to "Z".To define a character set consisting of a base group other than the character "M", use the[A-z-[m]]. to define a character set consisting of basic groups other than the character set "D", "J", and "P", use [A-Z-[DJP]]. to define a character set consisting of a base group other than the "M" to "p" character range, use [A-z-[m-p]].

Consider using a nested character class subtraction expression[A-z-[d-w-[m-o]]. The expression is calculated from the innermost character range.   First, the range of characters from "M" to "O" is subtracted from the range of characters from "D" to "w", which results in a character set from "D" to "L" and from "P" to "w".  [ABCMNOXYZ]. " > then subtract the collection from the range of characters from "a" to "Z", which produces the character set  [ABCMNOXYZ].

Any character class can be used for character class subtraction.  \s), the characters in the Punctuation General category (\p{p}), the Characters in the isgreek named block (\p{isgreek}), and The Unicode NEXT Line control character (\x85), use [\u0000-\uffff-[\s\p{p}\p{isgreek}\ X85]]. " To define a character set, the character set includes characters ( \p{p}) in addition to white-space characters ( \s), punctuation in the universal category, and isgreek  the characters in the named Block ( \p{isgreek}) and all from the Unicode NEXT line control character (\x85) from \ u0000 to \uffff Unicode characters, use  [\u0000-\uffff-[\s\p{p}\p{isgreek}\x85]].

Select the character class that will produce useful results for the character class subtraction expression.Avoid using an expression that produces an empty character set, which will not match anything, and avoid using an expression that is equivalent to the initial base group.For example, an expression[\p{isbasiclatin}-[\x00-\x7f]] subtracts all characters in the range of IsBasicLatin characters from the IsBasicLatin General category, and the result is an empty collection. Similarly, the result of an expression [a-z-[0-9]] is the initial basic group. This is because the base group (which is a character range of letters from "a" to "Z") does not contain any characters in the exclusion group (which is a range of characters from "0" to "9" in decimal numbers).

The following example defines the regular expression ^[0-9-[2468]]+$, which matches the 0 and odd numbers in the input string. The regular expression pattern can be interpreted as shown in the following table.

Elements

Description

^

The match starts at the beginning of the input string.

[0-9-[2468]]+

Matches one or more occurrences of any character (from 0 to 9, except 2, 4, 6, and 8). In other words, match one or more occurrences of 0 or an odd number.

$

Ends the match at the end of the input string.

 usingSystem;usingSystem.Text.RegularExpressions; Public classexample{ Public Static voidMain () {string[] inputs = {"123","13579753","3557798","335599901" }; stringPattern =@"^[0-9-[2468]]+$"; foreach(stringInputinchinputs) {Match Match=Regex.match (input, pattern); if(match. Success) Console.WriteLine (match.      Value); }         }}//The example displays the following output://13579753//335599901

Regular expressions-character class subtraction

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.