vb.net - 根据条件计算子字符串中字符串的出现次数

标签 vb.net string split substring

我需要计算数字在字符串中出现的频率。它应该计算前面有空格的每一次出现,除了后面跟着 = 的情况。

例如: 如果我需要知道这个字符串中有多少个“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/

相关文章:

vb.net - 数据表不知道其主键

vb.net - vb.net 中的 3D 文件

c# - 如何通过字符串中的单个单词匹配来提取整个句子?

python - 拆分一个不带空格的十进制值字符串 - Python

vb.net - 使用 ReadAsync 时发生类型 'System.AggregateException' 的异常

mysql - 参数化查询显示 "Mixing named and unnamed parameters is not allowed."

string - Stata:如何将字符串变量更改为日期?

c - 根据初始格式解析字符串

c++ - 使用 C++11 拆分字符串

python - 如何使用拆分或正则表达式从 python 中的字符串获取子字符串