嘿,我有以下字符串作为输入:
"abcol"
"ab_col"
"cold"
"col_ab"
"col.ab"
我有要搜索的字符串 col。我正在使用正则表达式来匹配
Match matchResults = Regex.Match(input , "col", RegexOptions.IgnoreCase);
我只想匹配具有这种模式的字符串 [任何特殊字符或无] + col + [任何特殊字符或无]
从上面的输入,我只想返回 ab_col, col_ab, col.ab
非常感谢任何帮助。
谢谢
[任何特殊字符] = [^A-Za-z0-9]
请您参考如下方法:
你可以使用这个正则表达式:-
(?:^.*[^a-zA-Z0-9]|^)col(?:[^a-zA-Z0-9].*$|$)
解释:-
(?: // non-capturing
^ // match at start of the string
.*[^a-zA-Z0-9] // match anything followed by a non-alphanumeric before `col`
| // or
^ // match the start itself (means nothing before col)
)
col // match col
(?: // non-capturing
[^a-zA-Z0-9].* // match a non-alphanumeric after `col` followed by anything
$ // match end of string
| // or
$ // just match the end itself (nothing after col)
)
