简体   繁体   中英

Regex searching for a letter starting a word and next word containing another word

How might I search through a list of names and only return the names that have a word starting with 's' and the next word starting with 'mary'?

For example, I have 2 titles: "Avera St. Mary's Hospital" and "Arthritis Care Specialists of Maryland". I search 'S Mary' and would like it to return "Avera St. Mary's Hospital" not "Arthritis Care Specialists of Maryland". My code returns both...Any help would be much appreciated!

var testList = new List<string>();
List<string> titles = new List<string>();
titles.Add("Avera St. Mary's Hospital");
titles.Add("Arthritis Care Specialists of Maryland");
foreach (var title in titles)
{
    var pattern = @"(?<!\w)s.*\smary";
    Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
    Match m = r.Match(title);
    if (m.Success)
    {
        testList.Add(title);
    }
}

You need to change your regular expression like:

var pattern = @"(?<!\\w)s\\w+[-| |~|@|(|)|.]*[\\s]+Mary";`

[-|`|~|@|(|)|.] specifies the special characters allowed between S* and Mary like St- Mary

Put a \\b — which means word boundary after mary .

demo

The .* is the problem within the regular expression given in the question. That .* matches too much text. (Changing it to a non-greedy .*? will not work.)

From the question and additional example in comments, the match should be of:

  • A word starting with s . The definition of "word" is not precise but using "any characters that are not spaces" matches the examples.
  • A separator between two words. Assume that one or more spaces is allowed.
  • A word starting with the letters mary . Anything may follow these four characters.

This leads to the simple regular expression: \\bs[^ ]* +mary

\b               A word boundary
s                This exact character
[^ ]*            Zero or more characters that are not spaces
 +               One or more spaces
mary             These exact characters

Combining and sorting the examples in the question and the comments gives these as example that should match:

Avera St. Mary's Hospital
Carondelet St. Mary's Hospital.
Centre Hospitalier St- Mary,
saint mary,
Saint Mary's Home of Erie,
st mary
st mary's
st. mary,

These are example that should not match:

Arthritis Care Specialists of Maryland
Cardiovascular Specialists Of Central Maryland,

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