簡體   English   中英

匹配特定字符串,該字符串是另一個字符串的子字符串

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM