


Detailed analysis of matching single characters using regular expressions
This time I will bring you a detailed analysis of using regular expressions to match a single character. What are the precautions for using regular expressions to match a single character? The following is a practical case, let’s take a look.
The example in this article describes the regular expressiontutorial on matching a single character. 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.
java test code:/** * 根据正则表达式和要匹配的源文本,输出匹配结果 * @param regex 正则表达式 * @param sourceText 要匹配的源文本 */ public static void matchAndPrint(String regex, String sourceText){ Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(sourceText); while(matcher.find()){ System.out.println(matcher.group()); } }
1. Match plain text
1. Only one matching result
First let’s look at a simple regular expression, today, although it is plain text itself, it is a regular expression. Let’s look at an example: Source text:Yesterday is history, tomorrow is a mystery, but today is a gift.
Regular expression:today
Result: Yesterday is history, tomorrow is a mystery, but[today] is a gift.
Analysis: The regular expression used here is plain text , which matches today in the source text. Call the matchAndPrint method, the output result is: today2. There are multiple matching results
Source text:Yesterday is history, tomorrow is a mystery, but today is a gift.
Regular expression:is
Result :Yesterday is history, tomorrow is a mystery, but[today] is a gift.
Analysis: In the source text, there are three is, but four is is output, because history is in will also be matched. Call the matchAndPrint method, the output result is:is
##is##is
is
3. Letter case problem
The regular expression is Letters are case-sensitive, but many regular expression implementations also support case-insensitive matching operations. In JavaScript
, use the i flag to perform a case-insensitive match.In java, if you want to be case-insensitive, when compiling the regular expression, you can specify:
Patternpattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
The regular expressions we saw earlier are all static plain text, and they do not reflect the power of regular expressions at all. Next, let's see how to use regular expressions to match unpredictable characters.
In regular expressions,Special characters
(or collections of characters) are used to give what to search for. The . character (English status period) can match any single character. Equivalent to the ? character in DOS and the _ (underscore) character in SQL. For example: the regular expression c.t will match cat, cut, cot, etc. Let’s look at an example.orders1.txt
##orders2.txt
sales1.txt
salesA.txt
##orders3.txt
sales2.txt
sales.txt
Regular expression:sales.
Result:orders1.txt
orders2.txt【sales1】.txt
【salesA】.txt
orders3.txt
【 sales2】.txt
【sales.】txt
Analysis: The regular expression sales. will combine stringsales and The file name composed of another note is found. From the results, it can be seen that. can match letters, numbers, and itself. 4 out of 7 files match this pattern.
If the matchAndPrint method is called, the output result is:
sales1
salesA
sales2
sales.
3. Match special characters
. Characters have special properties in regular expressions meaning. If you need a . in the pattern, you have to find a way to tell the regular expression that you need the . character itself rather than its special meaning in the regular expression. To do this, the . must be escaped by preceding it with a \ character. \ is also a metacharacter (metacharacter, indicating that this character has a special meaning, not the character meaning itself). Consider the following example.
Find files starting with na or sa, no matter what number follows it.
Text:
sales.txt
na1.txt
na2.txt
sa1.txt
sanatxt.txt
Regular expression: .a.. txt
Result:
[sal]es.txt
[na1].txt
【na2】.txt
【sa1】.txt
【sanatxt】.txt
Analysis: This regular rule found na1.txt, na2.txt, and sa1.txt, but also found two unexpected results. Because the . character in the regex .a..txt will match any character. To match the . character itself, you need to use \ escape. Modifying the regular expression to .a.\.txt can meet our needs.
Note: If you use java, then the regular expression .a.\.txt should be written as .a.\\.txt, because \ is also an escape character in the java language.
4. Summary
Regular expressions are often referred to as patterns. They are actually strings composed of some characters. These characters can be ordinary characters (plain text) or metacharacters (special characters with special meanings). Here is an introduction to how to use ordinary characters and metacharacters to match unit characters. .can match any character. \ is used to escape characters. In regular expressions, character sequences with special meanings always begin with the \ character.
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:
Using php and js to implement regular password matching of numbers and letters
Regular in JQ Verification cannot contain Chinese methods
The above is the detailed content of Detailed analysis of matching single characters using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Use Java's Character.isDigit() function to determine whether a character is a numeric character. Characters are represented in the form of ASCII codes internally in the computer. Each character has a corresponding ASCII code. Among them, the ASCII code values corresponding to the numeric characters 0 to 9 are 48 to 57 respectively. To determine whether a character is a number, you can use the isDigit() method provided by the Character class in Java. The isDigit() method is of the Character class

How to use AutoCorrect to type arrows in Word One of the fastest ways to type arrows in Word is to use the predefined AutoCorrect shortcuts. If you type a specific sequence of characters, Word automatically converts those characters into arrow symbols. You can draw many different arrow styles using this method. To type an arrow in Word using AutoCorrect: Move your cursor to the location in the document where you want the arrow to appear. Type one of the following character combinations: If you don't want what you type to be corrected to an arrow symbol, press the backspace key on your keyboard to

Your physical or numeric keyboard provides a limited number of character options on the surface. However, there are several ways to access accented letters, special characters, and more on iPhone, iPad, and Mac. The standard iOS keyboard gives you quick access to uppercase and lowercase letters, standard numbers, punctuation, and characters. Of course, there are many other characters. You can choose from letters with diacritics to upside-down question marks. You may have stumbled upon a hidden special character. If not, here's how to access them on iPhone, iPad, and Mac. How to Access Extended Characters on iPhone and iPad Getting extended characters on your iPhone or iPad is very simple. In "Information", "

Correctly displaying Chinese characters in matplotlib is a problem often encountered by many Chinese users. By default, matplotlib uses English fonts and cannot display Chinese characters correctly. To solve this problem, we need to set the correct Chinese font and apply it to matplotlib. Below are some specific code examples to help you display Chinese characters correctly in matplotlib. First, we need to import the required libraries: importmatplot

A superscript is a character or characters, either letters or numbers, that you need to set slightly above the normal line of text. For example, if you need to write 1st, the letter st needs to be slightly higher than the character 1. Likewise, a subscript is a group of characters or a single character and needs to be set slightly lower than normal text level. For example, when you write a chemical formula, you need to place the numbers below the normal line of characters. The following screenshots show some examples of superscript and subscript formatting. Although it may seem like a daunting task, applying superscript and subscript formatting to your text is actually quite simple. In this article, we will explain in some simple steps how to easily format text using superscript or subscript. Hope you enjoyed reading this article. How to apply superscript in Excel

How to use Golang to determine whether a character is a letter. In Golang, determining whether a character is a letter can be achieved by using the IsLetter function in the Unicode package. The IsLetter function checks whether the given character is a letter. Next, we will introduce in detail how to use Golang to write code to determine whether a character is a letter. First, you need to create a new Go file in which to write the code. You can name the file "main.go". code

The character representation of the Enter key in Java is `. In Java, ` represents a newline character, and when this character is encountered, the text output will wrap. Here is a simple code example that demonstrates how to use `` to represent the Enter key: publicclassMain{publicstaticvoidmain(String[]args){System.out.println("This is the first line of this

Enable touch keyboard in tablet mode If you have a touch screen laptop, you can use the touch keyboard to type multiple special characters on Windows 11. This is probably the easiest way to add special characters. Enable special characters for touchscreen on Windows 11: Open the Start menu and select Settings. When Settings opens, navigate to Time & Language > Typing > Touch Keyboard. In the Typing menu, check the "Show touch keyboard when no keyboard is available" option. Enable touch keyboard without tablet mode Another way to access the touch keyboard is to make it appear on the taskbar full-time. To make the touch keyboard accessible, you need to tell Windows 11 to display it. Use the following steps: From the Start menu, select
