search
HomeBackend DevelopmentPHP Tutorial新手请教:正则表达式反向引用的疑问

各位前辈,大家好!我是菜鸟,有事请教。

我最近在看《细说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'。看来书不能全信呀
十分感谢两位!

Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

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.