嘿,我有以下字符串作为输入:
"abcol"
"ab_col"
"cold"
"col_ab"
"col.ab"
Run Code Online (Sandbox Code Playgroud)
我有要搜索的字符串col.我正在使用正则表达式匹配
Match matchResults = Regex.Match(input , "col", RegexOptions.IgnoreCase);
Run Code Online (Sandbox Code Playgroud)
我想只匹配具有此模式的字符串
[Any special character or nothing ] + col + [Any special character or nothing]
从上面的输入,我只想返回
ab_col, col_ab , col.ab
任何帮助都非常感谢.
谢谢
[任何特殊字符] = [^ A-Za-z0-9]
你可以使用这个正则表达式: -
(?:^.*[^a-zA-Z0-9]|^)col(?:[^a-zA-Z0-9].*$|$)
Run Code Online (Sandbox Code Playgroud)
说明: -
(?: // 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)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6120 次 |
| 最近记录: |