search
HomeBackend DevelopmentPHP Tutorial 正则表达式N个有关问题

正则表达式N个问题
嗯嗯,如前些天的帖子所述,我已经开始学正则了
自己摸索了三两天,现在有些实在是搞不清楚的问题
我现在手上这本书讲得不怎么详细,我是对着百度百科来学的
言归正转
=====================================
=====================================
问题1:
我想问,正则表达式一般都需要用

/ 包住内容吗? /

而如果有修饰符的话,修饰符就写在第二个 / 号后面是吗?(比如 /aAa/i 以i作为修饰符才放第2个 / 号后面)
我是看着百度百科来学的,它这样说到一段:

$
匹配行结束符。例如正则表达式weasel$ 能够匹配字符串"He's a weasel"的末尾
但是不能匹配字符串"They are a bunch of weasels."


于是我用PHP写了下代码调试,
$str2="He's a weasel";
$pm2='weasel$';

if(preg_match($pm2,$str2))
{
echo 'True
';
}
else
{
echo 'False
';
}

但结果却是失败
鼓捣了好久,我终于把
$pm2='weasel$';
改成
$pm2='/weasel$/';

成功了,百科却没有说这样..
可能我没认真看啥的
在这确认一下,搜索的内容用 / 号在左右包住,修饰符在第2个/号后面是吗?压根连书写规则都没搞清楚呢我...别说匹配规则


=====================================
=====================================
问题2:
百科中第一个提到"元字符"的表格里面,第四行提到的 * 号作用,用 .* 匹配我是理解了,但是后面它又这么说"比如.* 可以匹配不管是什么 "

于是我用以上代码测试,却显示 找不到
~~~~~~~~~~~~~~~~~~~~~~
$str1=
test;

$pm1='/.*/';

if(preg_match($pm1,$str1))
{
echo '1.找到了
';
}
else
{
echo '1.找不到
';
}



~~~~~~~~~~~~~~~~~~~~~~
这里嘛,它说的 .* 中包含 / ,我意识到 / 可能导致被误认为是表达式终止符,于是换成了 \/ ,然而也不行,另外其实我将它的描述理解为: 这里面的内容 可以被 .* 这个表达式匹配出来,然而我却不知道怎么写这个表达式

=====================================
=====================================
问题3:
其实我将 \ 理解为转义符,对吗?感觉百科里说法不是很明朗就像C语言里或PHP里的, \n 被转义为 回车符,
\t 是Tab制表符, \\ 是 \ , \/ 就是 / ,\$ 就是 $ 什么的


=====================================
=====================================
问题4:
以下代码是根据百科 字元符 表的第7行所述'例如正则表达式\能够匹配字符串"for the wise"中的"the"'

$str1=for the wise
test;

$pm1='/\/';
if(preg_match($pm1,$str1))
{
echo '1.找到了
';
}
else
{
echo '1.找不到
';
}

找不到的原因是不是因为他后面还加了句"这个元字符不是所有的软件都支持的"

=====================================
=====================================
问题5:
关于 ? 号的介绍,百度的完全看不懂,又找了下其它文献
我理解为 app?path 的 ? 号前面的 app 三个字母在path前面出现一次就就能匹配,但是我无法理解这样设置字符

$str1=abcdefg
test;

$pm1='/df?e/';
if(preg_match($pm1,$str1))
{
echo '1.找到了
';
}
else
{
echo '1.找不到
';
}

却也能匹配!哪里有df 啊,连个f都在e的后面,这里开始导致我感觉自己白学了似的,好像还没理解正则的匹配规则!
  



------解决方案--------------------
看了一半。你就这么理解

/告诉正则 条件开始了 第二个/ 告诉正则 正则条件结束了。就是了其实 这仅仅 是一个组合 你甚至可以用##或者其他字符来代替这个所谓的开始和结束。

在这里面 如果 / xxxxa/xxxxb/ 这个时候 正则会以为到了 xxxxa的时候就结束了,后面就会出错,所这个时候我们就需要转义让它明白 那只是中间的一个字符而不是结束。 这就是为什么要用到转义

$str1=
test;

$pm1='/.*/';

这里 的匹配条件是 以 开始的 任意字符 然后一直到 结束 你的 str1 没有
结束 他当然不匹配。
剩下的没看了,让其他人告诉你吧 哈哈
------解决方案--------------------
问题一:
/这里才是真正的正则/
正则都需要用 // 包着在中间,如果有修正符的,加在最后一个/后面
如:preg_match('/aaa$/is') 这里的is是修正符,至于每个是啥意思,你得查了
当然这里要注意一下,//里包着的正则如果也含有/这个东西,需要转义的
还注明一下,这里是用//包着正则,也可以用#@之里的包着,你可以试试

问题二:
你这里的当然是找不到的了,你要找的是 以及中间的内容,你给出的内容只有 ”“,肯定不符合要求了,找不到是正常,找到了是你rp出现问题。

问题三:
像问题一我所说的,你用/包着正则,如果你的正则里也出现/,当然要转义了,还有,$^?之类的都是特殊字符,你想直接匹配那特殊字符,当然也要转义了,不然计算机会当是特殊字符去处理
例子:
内容:aaabbb$ccc
如果你想匹配bb$cc 是否符合
1:/bb$cc/ 这当然是有问题的,看字面是与上面的符合,但计算机会把$这个当作最后的意思
2:/bb\$cc/ 这个是正确的

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment