Home Web Front-end JS Tutorial How to write regex to match a group of characters

How to write regex to match a group of characters

Mar 30, 2018 am 09:51 AM
match talent

This time I will show you how to write a regular expression to match a group of characters. What are the precautions for matching a group of characters with a regular expression? The following is a practical case, let's take a look.

The example in this article describes the method of matching a group of characters in the

regular expressiontutorial. Share it with everyone for your reference, as follows:

Note: In all examples, the regular expression matching results contain [ and ]## in the source text. #, some examples will be implemented using Java. If it is the usage of regular expressions in Java itself, it will be explained in the corresponding place. All java examples are tested under JDK1.6.0_13.

1. Match one of multiple characters

A match in the previous article "Regular Expression Tutorial: Detailed Explanation of Matching a Single Character" In the example of a text file starting with na or sa, the regular expression used is .a.\.txt. If there is another file called cal.txt, it will also be matched. What should I do if I only want to match files starting with na or sa?

Since we only want to find n or s, using one that can match any character is obviously not possible. In regular expressions, we can use [and] to define a

character set

combination. In the character set defined using [and], all characters between these two metacharacters are the A component of a set. The matching result of a character set is text that can match any member of the set. Let’s look at an example similar to the previous one:

Text:

sales.txt

na1 .txt

na2.txt

sa1.txt

sanatxt.txt

cal.txt

Regular expression:

[ns]a.\.txt

Result:

sales.txt

【na1.txt】

【na2.txt】

【sa1.txt】

sanatxt.txt

##cal.txt

Analysis : The regular expression used here starts with [na]. This set will match the characters n or s and will not match any other characters. [ and ] do not match any characters; they only define a set of characters. Next, a matches a character a, \. will match a . character itself, txt matches the txt character itself, and the matching results are consistent with our expectations.

However, if one of the files is usa1.txt, then it will also be matched. This is a problem of positional matching, which will be discussed later.

2. Use the character set interval

In the above example, what if we only want to match files that start with na or sa and are followed by a number? In the regular expression [ns]a.\.txt, . will match any character, including numbers. This problem can be solved using the character set:

sales.txt

na1.txt

na2. txt

sa1.txt

san.txt

sanatxt.txt

cal.txt

Regular expression: [ns]a[0123456789]\.txt

Result:

sales.txt

【na1.txt】

【na2.txt】

【sa1.txt】

san.txt

sanatxt.txt

cal.txt

Analysis: As you can see from the results, we only match those starting with na or sa , followed by a number file, and san.txt was not matched because the character set [0123456789] was used to limit the third character to only a number.

In regular expressions, some character intervals are frequently used, such as 0-9, a-z, etc. In order to simplify the definition of character intervals, regular expressions provide a special metacharacter - to Define character range. Like the example above, we can use regular expressions to match: [ns]a[0-9]\.txt, and the result is exactly the same as above.

The character range is not limited to numbers. The following are legal character ranges:

[A-F]: Matches all uppercase letters from A to F.

[A-Z]: Matches all uppercase letters from A to Z.

[A-z]: Matches all letters from ASCII character A to ASCII character z. But this interval is generally not used, it is just an example. Because they also contain characters such as [ and ^, which are arranged between Z and a in ASCII.

The first and last characters of the character interval can be any character in the ASCII character list. But in actual use, the most commonly used ranges are numbers and alphabetic characters.

Note: When defining a character interval, the last character of the interval cannot be smaller than the first character (such as [9-0]). This is not allowed. - as a metacharacter can only appear between [ and ], if it is anywhere outside [ and ], it is just an ordinary character and will only match - itself.

Multiple character ranges can be given in the same character set. For example: [0-9a-zA-Z] will match any uppercase and lowercase letters and numbers.

Let’s look at an example of matching colors in a web page:

Text:

<span style="background-color:#3636FF;height:30px; width:60px;">测试</span>
Copy after login

Regular expression: #[0-9A-Fa-f] [0 -9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f]

Result:【#3636FF】;height:30px; width:60px;">Test

Analysis: In web pages, color is generally expressed as an RGB value starting with #, R represents red, G represents green, and B represents blue. Any color can be blended through different combinations of RGB. RGB values ​​are represented by hexadecimal values, such as #000000 representing white, #FFFFFF representing black, and #FF0000 representing red. Therefore, the regular expression for matching colors in web pages starts with #, followed by the same set of 6 [0-9A-Fa-f] characters (this can be abbreviated as #[0-9A-Fa-f]{6}, This will be discussed later in Repeat Matching).

3. Get non-matching

Character sets are usually used to specify a set of characters that must match one of them, but in some cases, we need to do the opposite. , gives a set of characters that do not need to be obtained. In other words, except for the characters in that character set, any other characters can be matched.

For example, to match files that begin with na or sa and are not followed by numbers:

Text:

sales.txt

na1.txt

na2.txt

sa1.txt

sanatxt.txt

san.txt

Regular expression: [ns]a[^0-9]\.txt

Result:

sales.txt

na1.txt

na2. txt

sa1.txt

sanatxt.txt

【san.txt】

Analysis: The pattern used in this example is exactly the opposite of the previous one. The previous [0-9] only matched numbers, but here [^0-9] matched non-numbers.

Note: ^ between [and] means negation. If it appears at the beginning of the regular expression, it means that the positional match is matched, which will be discussed later. At the same time, the effect of ^ will apply to all characters or character intervals in a given character set, not just the character or character interval immediately following the ^ character. For example, [^0-9a-z] means it does not match any numbers or lowercase letters.

4. Summary

Metacharacters [and] are used to define a set of characters, and their meaning is that they must match one of the characters in the set. There are two ways to define a character set: one is to list all characters; the other is to use metacharacters - given in the form of character intervals. Character sets can be negated using the metacharacter ^, which will forcibly exclude the given character set from the matching operation. Except for the characters in the character set, other characters can be matched.

In the next article, we will discuss the use of some metacharacters in regular expressions.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JS password strength verification regular expression (with code)

Regular expression in JQ Verification cannot contain Chinese methods

The above is the detailed content of How to write regex to match a group of characters. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Practical Guide to Regular Expressions in Go: How to Match Hexadecimal Color Codes Practical Guide to Regular Expressions in Go: How to Match Hexadecimal Color Codes Jul 13, 2023 am 10:46 AM

Go Language Regular Expressions Practical Guide: How to Match Hexadecimal Color Codes Introduction: Regular expressions are a powerful and flexible tool for pattern matching and finding strings. In Go language, we can use the built-in regular expression package regexp to implement these operations. This article will introduce how to use regular expressions to match hexadecimal color codes in Go language. Importing the regular expression package First, we need to import the regular expression package regexp of the Go language. You can add the following import statement at the beginning of the code: i

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

PHP regular expression in action: matching letters and numbers PHP regular expression in action: matching letters and numbers Jun 22, 2023 pm 04:49 PM

PHP regular expression practice: matching letters and numbers Regular expression is a tool used to match strings, which can easily realize string search, replacement, split and other operations. Regular expressions are also a very useful tool in PHP development. This article will introduce how to use PHP regular expressions to match letters and numbers. Matching a Single Character To match a single character, you can use the character classes in regular expressions. Character classes are represented by square brackets []. The characters in them represent the characters that can be matched. You can use hyphens - to represent ranges.

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

How to match in Jedi Submarine 2 How to match in Jedi Submarine 2 Feb 27, 2024 pm 08:43 PM

Jedi Submarine 2 is a third-person shooting game with high-quality masterpiece gameplay. It has a lot of exciting gameplay that allows friends to explore the operational fun of online shooting battles. The online mode in the game can be matched. Some players I still don’t know how to operate matching. In this issue, I will share the matching steps with you! Matching operation tutorial of Jedi Submarine 2. Answer: Click Quick Match on the planet interface. The matching method of Jedi Submarine 2. The quick matching of Jedi Submarine 2 is a very good function. It can help players find teammates to match together, enter a mission together, and cooperate with each other to obtain a higher mission evaluation. The matching options are on the planet interface. When looking for tasks or viewing public rooms, there will be a quick match below. Click to start matching. If the player turns on cross leveling

Type mismatch in Java - java.lang.ClassCastException Type mismatch in Java - java.lang.ClassCastException Jun 24, 2023 pm 09:30 PM

As a strongly typed language, Java requires that the types of variables must be clearly determined at compile time, which ensures the security of the program to a certain extent. But sometimes, at runtime, we may encounter a type conversion exception - java.lang.ClassCastException. This exception will appear in a Java program. When the program tries to convert an object to an incompatible type, This exception will be thrown. Java.lang.ClassCastExcepti

PHP Regular Expression: How to match all textarea tags in HTML PHP Regular Expression: How to match all textarea tags in HTML Jun 22, 2023 pm 09:27 PM

HTML is a commonly used page markup language used to display content on web pages. In HTML, the textarea tag is used to create text boxes that allow users to enter or edit text. When you need to extract all textarea tags and their contents from a page, PHP regular expressions can provide a simple and effective solution. In this article, we will learn how to match all textarea tags in HTML using PHP regular expressions. Understand regular tables

How to use regular expressions in PHP to match multiple consecutive specific characters How to use regular expressions in PHP to match multiple consecutive specific characters Jun 22, 2023 pm 08:15 PM

Regular expressions are a powerful text processing tool that are widely used in PHP. One common usage is to match multiple consecutive specific characters, such as matching multiple consecutive spaces, multiple consecutive commas, etc. This article will introduce how to use regular expressions in PHP to achieve this function. In PHP, we can use the preg_match() function to perform regular expression matching. This function requires two parameters: the regular expression and the string to be matched. If the match is successful

See all articles