简体   繁体   中英

Finding match maybe using regex

I'm trying to find match between 2 strings, eg:

string entry = "AB"
string string1 = "ABC"
string string2 = "AB_"
string string3 = "AB"

The expected result from the comparison are:

entry compares string1 return false
entry compares string2 return true
entry compares string3 return true

I managed to achieve the latter 2 conditions but not the first condition. The regular expression I used to check the match is \\bAB\\b but the comparison between entry and string1 returns true, but I expect a false there.

So what I understood from your example or condition is, for true match, the strings may be same as the entry or may have an additional underscore after the entry string. If that so, then you may try this:

^AB_?$

Regex Demo

string pattern = @"^AB_?$";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM