对正则表达式量词的困惑? [英] Confused about regex quantifiers?
问题描述
我正在教我自己如何做量词{2}所以我读到这是基于恰好2次出现但我的另外说这是没有提到2次为什么会这样?
I'm teaching my self on how to do a quantifier {2} so I read that this is based on exactly 2 occurrences but my if else is saying this is not mentioned 2 times why is that?
<?php
$string = 'sasa sasa';
if(preg_match('/sasa{2}/',$string))
{
echo 'sasa is mentioned 2 times';
}
else
{
echo "sasa is not mentioned 2 times";
}
?>
< b>我尝试了什么:
谷歌搜索,但我找不到那么容易理解的信息。
What I have tried:
Google search but I can't find that much easy to understand info on this.
推荐答案
string = ' sasa sasa';
if(preg_match(' / sasa {2} /',
string = 'sasa sasa'; if(preg_match('/sasa{2}/',
string))
{
echo ' sasa被提及2次';
}
其他
{
echo sasa未提及2次;
}
?>
string)) { echo 'sasa is mentioned 2 times'; } else { echo "sasa is not mentioned 2 times"; } ?>
< b>我尝试了什么:
谷歌搜索,但我找不到那么容易理解的信息。
What I have tried:
Google search but I can't find that much easy to understand info on this.
你正在错误地使用量词。
You are doing wrong usage of quantifier.
/sasa{2}/
只有当字符串包含sasaa时才会匹配
如果你想知道字符串中sasa是否是2次,你需要使用/ sasa /并询问匹配数量。 />
只是一些有趣的链接,可以帮助构建和调试RegEx。
以下是RegEx文档的链接:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
.NET Regex Tester - 正则表达式风暴 [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:Learn,Build,&测试RegEx [ ^ ]
此节目RegEx是一个很好的图表,它非常有助于理解RegEx的作用:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
will match only if string contain "sasaa"
if you want to know if "sasa" is 2 times in a string, you need to use "/sasa/" and ask number of matches.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
这篇关于对正则表达式量词的困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!