我需要计算数字在字符串中出现的频率。它应该计算前面有空格的每一次出现,除了后面跟着 =
的情况。
例如:
如果我需要知道这个字符串中有多少个“1”: this is a 1 ramdnom string with 2 个数字 1 with 1=something
它应该返回 2,因为第三个后面跟着一个=
要查找出现的次数,我使用的是:occurrences = mystring.Split("1").Length - 1
但是如何排除那些后面跟着 =
的内容呢?
谢谢
最佳答案
类似于,
Dim occurrences = Regex.Matches(yourString, "\W[0-9]([^=]|$)").Count
如果您想进行替换,请使用 Regex.Replace
过载。
分解一下,这个表达式匹配
\W // any whitespace character
[0-9] // any deciaml digit
( // either
[^=] // not =
| // or
$ // the end of the string
)
关于vb.net - 根据条件计算子字符串中字符串的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22913697/