[英]Regex match a substring if that substring is not preceded by a specific string?
[英]Match a specific string that is a substring of a another string
我有以下代碼
// the values for the input and the pattern
// are combinations of R, NR and HR
var input = "NR|HR";
var pattern = "R";
var isMatch = Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
這返回true
因為NR and HR
包含R
有沒有什么方法可以使R
與正則表達式完全匹配?
您可以使用單詞邊界:
var pattern = "\bR\b";
字符串^的開始和字符串$的結束
var pattern =“^ R $”;
請嘗試以下方法:
'(^R\|)|(\|R$)|(\|R\|)|(^R$)'
它從以下選擇我們的R.
R|NR
NR|R
R
但不是以下
HR|NR
HR
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.