How to match numbers with regular expressions (with code)
This time I will bring you Regular expressionsHow to match numbers (with code), what are the precautions for regular expressions to match numbers, the following is a practical case, one Get up and take a look.
Regular expressions are used in string processing, form validation and other occasions, and are practical and efficient. Now we collect some commonly used expressions here for emergency use.
Regular expression for matching Chinese characters: [\u4e00-\u9fa5]
Comment: Matching Chinese characters is really a headache. With this expression, Easy to handle
Matches double-byte characters (including Chinese characters): [^\x00-\xff]
Comment: Can be used to calculate the length of a string (a double-byte Character length is counted as 2, ASCII characters are counted as 1)
Regular expression matching blank lines:\n\s*\r
Comment: Can be used to delete blank lines
Regular expression matching HTML tags: ]*>.*?\1>|<.>
Comment : The version circulating on the Internet is too bad. The above one can only match part of it, and it is still powerless for complex nested tags.
Regular expression for matching leading and trailing whitespace characters: ^\s*|\s*$
Comment: It can be used to delete whitespace characters at the beginning and end of the line (including spaces, tabs, form feeds, etc.), a very useful expression
Regular expression that matches the email address :\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Comment: It is very difficult to validate the form. Practical
Regular expression for matching URL: [a-zA-z]+://[^\s]*
Comment: The version circulating on the Internet has very limited functions. The above one Basically can meet the needs
Match whether the account is legal (starting with a letter, 5-16 bytes are allowed, alphanumeric underscores are allowed): ^[a-zA-Z][a-zA-Z0-9_]{4 ,15}$
Comment: Very practical during form verification
Matching domestic phone numbers:\d{3}-\d{8}|\d{4}-\d{7 }
Comment: The matching format is such as 0511-4405222 or 021-87888822
Matching Tencent QQ number: [1-9][0-9]{4,}
Comment: Tencent QQ number starts from 10000
Matches Chinese postal code: [1-9]\d{5}(?!\d)
Comment: Chinese postal code is 6 digits
Matching ID card:\d{15}|\d{18}
Comment: China’s ID card is 15 or 18 digits
Matching ip address:\ d+\.\d+\.\d+\.\d+
Comment: Useful when extracting IP address
match specific numbers:
^[ 1-9]\d*$ // Match positive integers
^-[1-9]\d*$ // Match negative integers
^-?[1-9]\d*$ // Match Integer
^[1-9]\d*|0$ // Matches non-negative integers (positive integers + 0)
^-[1-9]\d*|0$ // Matches non-positive integers (Negative integer + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //Match positive floating point numbers
^- ([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //Match negative floating point numbers
^-?([1-9]\ d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //Match floating point number
^[1-9]\d* \.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ // Match non-negative floating point numbers (positive floating point numbers + 0)
^(- ([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //Match non-positive floating point numbers (negative Floating point number + 0)
Comment: Useful when processing large amounts of data, pay attention to corrections during specific applications
Match specific strings:
^[A -Za-z]+$ // Matches a string consisting of 26 English letters
^[A-Z]+$ // Matches a string consisting of 26 uppercase English letters
^[a-z]+ $ // Matches a string consisting of 26 lowercase English letters
^[A-Za-z0-9]+$ // Matches a string consisting of numbers and 26 English letters
^\w+ $ //Match a string consisting of numbers, 26 English letters or underscores
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:
Detailed graphic explanation of using regular multi-line mode and single-line mode
Regular non-capturing group Detailed explanation of using capture groups
The above is the detailed content of How to match numbers with regular expressions (with code). For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
