新手请教:正则表达式反向引用的疑问
各位前辈,大家好!我是菜鸟,有事请教。
我最近在看《细说PHP》,看书里讲,正则表达式里的每个子表达式(用()包含起来的),在不添加任何非捕获元字符的情况下,是按照从左至右,每个子表达式的匹配结果存进缓冲区,并用编号\1至\99引用,不知道我这么描述对不对。现在有两个问题:
1、书上说,使用编号引用子表达式,要在编号前加反斜线转义,这条很纠结,我试的是不需要的,是不是跟什么版本有关系?
2、表达式中,某个子表达式加了元字符?:,这样就不会存储匹配结果,那么缓冲区的编号就不会加一,也就没办法用编号来引用,那么,可以重复调用这个子表达式吗?
感谢!
回复讨论(解决方案)
你先举例说明 是不需要的
感谢版主回复。我在PHP里,用preg_match做匹配,模式是'^([a-zA-Z]*)\d+\1$',就是想匹配字母+数字+字母,匹配字符串'demo123demo'返回yes。如果在模式里的编号前添加反斜线,就返回false了。
上面是我自己试的。然后我百度了几个“正则表达式参考手册”,几个的例子里都是不用添加的。
附链接(不知道贴链接会不会违反发帖守则):https://translate.google.com.hk/?hl=zh-CN&tab=wT,里面第6点的例子是没有的。
好吧这个问题我只是想扩展一下确定是不是存在“版本差异”的问题,我真正迫切想得到答案的是第二个问题,麻烦版主了!
重复调用跟引用是一个意思吗? 如果是的话,你不是知道答案了吗。
/^([a-zA-Z]*)\d+ \1$/
这不就是 在编号前加反斜线转义 吗?
你的第二问是这个意思吗?
/^(?=[a-zA-Z]+)\d+(?=[a-zA-Z]+)$/
哈哈,这么一来就明白了。荣幸荣幸两位版主都出来了。
第一个加反斜线转义,我看的那本书上写的是编号本身就是\1到\99,要引用时要再加一条反斜线,也就是'\\1',书上的实例就是这样,昨晚感觉不对,才发现再加一条反斜线就是匹配字符串'\1'。看来书不能全信呀
十分感谢两位!

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











Mistlock Kingdom is an open world game where players can play as Sons of Fire to survive and explore. The game combines the unique entertainment of action RPG challenges, bringing players endless surprises and joy. In the game, players can explore resources, environments, weapons and more. Some novice players may be curious about how to get started with the game. In this introduction and sharing, we will provide you with some relevant getting started guides. Tips for Beginners to the Fog Lock Kingdom: The danger levels of areas shrouded by miasma are different. During the exploration process, new areas of the map will be gradually unlocked, and the location of the areas shrouded by miasma can be seen. The map will be distinguished by two colors. The blue area can be entered in a short time. The time you can stay will also be different depending on the character's ability level.

Anchor Arrival is a 3D turn-based card game with a high-definition beautiful girl two-dimensional theme. It provides a rich and exciting combination of characters for players to explore and experience. It has many powerful combinations of high-quality lineups. New players are also curious novices. What powerful characters are recommended in the pool? Let’s take a look at the selection reference for novices to win ten consecutive golds! Anchor Point Advent is a powerful character recommendation for novice pools. The first ten-consecutive pick is Alice. She is mainly a single-target lightning-type burst character. The output is very explosive, and the experience will be very friendly to newcomers, so it is highly recommended to choose it. It is recommended to choose the combination of "Alice" + "Antelope" for 10 points. Alice is the most worthy character to output the goldpire attribute, and is not even a bit stronger than the other two characters in the novice card pool. Alice can pass special

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

Crown of the Ancients is a high-quality and strategic card mobile game based on Western magical adventure. In-game secret exploration, ruins adventure, national championship and other special gameplay are waiting for you to experience. So for novice players, if they want to get started with this game quickly, a novice guide is indispensable. Today, the editor will bring you the relevant guide, let’s take a look. An overview of the Ancient Crown beginner’s guide, gameplay and area opening styles: 1. Diamond accumulation flow: Everything is focused on accumulating diamonds, and then you start to work hard after leaving the village. Except for the main magic weapon, three flywheel activities, etc., which require diamonds, the others are ignored. The main focus is on one of them. Don't pay attention to the hero challenge. You can fight as many as you can, and don't force it. Advantages: You only need to mess around to accumulate diamonds. After leaving the village, you can quickly access the latest activities with diamonds, get new series of hardware heroes, and diamonds are broken.

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

Phantom Beast Parlu is an open world survival game that supports multiplayer online. The game has a wealth of ways to collect creatures, and players can engage in various explorations such as combat, construction, and survival. For novice players, it is very important to master the operating skills and gameplay content of the game. Here are some newbie strategy tips for reference. 1. Learn basic operations: Before starting the game, it is recommended to learn the basic operations of the game, including movement, attack, collection, etc. This allows for better control of the character's operations. 2. Explore the world: The world of Phantom Beast Parlu is very vast, with many hidden places and resources waiting for players to discover. Don’t be afraid to take risks, try to explore every corner and find the Phantom Beast Palu. Quick tips for newbies. 1. Base site selection, put flatness first. In the Phantom Beast Pallu, choose a

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.
