我试图找到字符串中的句点数 [英] I am trying to find the number of periods in a string
问题描述
我试图找到一个字符串中的句点数(。)
我应该得到140当我应该只得到2.
我不明白原因。
我猜它不能用特殊字符工作但是用字母工作。
我尝试了什么:
string period =。;
正则表达式searchFor =新正则表达式(期间);
int numOfPeriod = searchFor.Matches(SBDuties).Count;
Response.Write(句号+< br />);
Response.Write(句号数+ numOfPeriod +< br />);
Response.Write(SBDuties);
Quote:我试图在字符串中找到句点数(。)
你正在使用RegEx,碰巧有些字符在RegEx中有特殊含义,你很幸运,因为' 。'是一个匹配任何字符的特殊字符。
正则表达式是一个功能强大的工具,但与任何工具一样,你需要学习如何使用它。 />
只是一些有趣的链接,可以帮助构建和调试RegEx。
以下是RegEx文档的链接:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]
Expresso正则表达式工具 [ ^ ]
这个显示RegEx是一个不错的图表对于了解RegEx的作用非常有用:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
除了RegEx之外还有其他选项可以很好地运行。请参阅 c# - 您如何计算a的出现次数?字符串中的字符串? - 堆栈溢出 [ ^ ]有几个不错的选择。
string period =[。];
I am trying to find the number of periods(.) in a string
I am getting 140 when I should when I should only get 2.
I do not understand the reason why.
I guessing it don't work with special characters but work with letters.
What I have tried:
string period = "."; Regex searchFor = new Regex(period); int numOfPeriod = searchFor.Matches(SBDuties).Count; Response.Write(period +"<br/>"); Response.Write("Number of full stops " + numOfPeriod +"<br/>"); Response.Write(SBDuties);
Quote:I am trying to find the number of periods(.) in a string
You are using RegEx, it happen that some chars have a special meaning in RegEx, and you are lucky because '.' is a special char that match any char.
Regex is a powerful tool, but like with any tool, you need to learn how to use it.
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[^]
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.[^]
There are other options besides RegEx that perform very well. See c# - How would you count occurrences of a string within a string? - Stack Overflow[^] for several good alternatives.
string period = "[.]";
这篇关于我试图找到字符串中的句点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!