简体   繁体   English

正则表达式匹配字符串中间的子字符串

[英]Regex Match substring in middle of string

I need help completing a regex expression.我需要帮助完成正则表达式。 I have the following regex: /(?i)encntr(?-i)/gi我有以下正则表达式:/(?i)encntr(?-i)/gi

https://regex101.com/r/t4pUkr/1/ https://regex101.com/r/t4pUkr/1/

I have the following three filenames:我有以下三个文件名:

  • 20200207050000-20200207162558-encntr.txt - should match using encntr 20200207050000-20200207162558-encntr.txt - 应该使用 encntr 匹配
  • 20200207050000-20200207162558-encntrprov.txt - should NOT match using encntr 20200207050000-20200207162558-encntrprov.txt - 不应该使用 encntr 匹配
  • 20200207050000-20200207162558-encntrlocation.txt - should NOT match using encntr 20200207050000-20200207162558-encntrlocation.txt - 不应该使用 encntr 匹配

My above regex matches all 3 but I only want it to match the first bullet point.我上面的正则表达式匹配所有 3 个,但我只希望它匹配第一个要点。 So I'm trying to use the ^(not) operator to achieve this but I'm not getting anywhere.所以我试图使用 ^(not) 运算符来实现这一点,但我没有得到任何地方。 Ignore the previous sentence, my understanding using ^ was ill-informed.忽略上一句,我对使用 ^ 的理解是错误的。 Additionally it would be more robust if I could do this without depending on the - and .此外,如果我可以在不依赖于 - 和 . that encntr, encntrprov, and encntrlocation are wrapped in. I do not author the files and can't guarantee the author follows a naming convention. encntr、encntrprov 和 encntrlocation 被包裹在里面。我不创作这些文件,也不能保证作者遵循命名约定。 Thus I cannot not guarantee the - and .因此我不能保证 - 和 . will always be there.将永远在那里。

Example of possible other file names:可能的其他文件名示例:

  • 20200207050000-20200207162558encntr.txt - should match using encntr 20200207050000-20200207162558encntr.txt - 应该使用 encntr 匹配
  • 20200207050000-20200207162558encntr01.txt - should match using encntr 20200207050000-20200207162558encntr01.txt - 应该使用 encntr 匹配
  • 20200207050000-20200207162558_encntr_.txt - should match using encntr 20200207050000-20200207162558_encntr_.txt - 应该使用 encntr 匹配
  • 20200207050000-20200207162558^encntr^.txt - should match using encntr 20200207050000-20200207162558^encntr^.txt - 应该使用 encntr 匹配
  • 20200207050000-20200207162558FinalencntrUploaded.txt - should match using encntr 20200207050000-20200207162558FinalencntrUploaded.txt - 应该使用 encntr 匹配

The point of using this regex is to remove the inner if statement from my code.使用这个正则表达式的目的是从我的代码中删除内部 if 语句。 I am iterating over enum values and checking if the enum value is present in the incoming filename.我正在迭代枚举值并检查传入文件名中是否存在枚举值。

Right now the code uses txtFilename.Contains(possibleIdentifierLower, StringComparison.OrdinalIgnoreCase)) to see if the incoming filename contains one of enum values.现在代码使用 txtFilename.Contains(possibleIdentifierLower, StringComparison.OrdinalIgnoreCase)) 来查看传入的文件名是否包含枚举值之一。 But in the case of enum encntr, the Contains method returns true for encntr, encntrprov, and the encntrlocation filename.但是对于 enum encntr,Contains 方法为 encntr、encntrprov 和 encntrlocation 文件名返回 true。 So I have to do an additional check to figure out which one it is.所以我必须做额外的检查,以确定它是哪一个。 I want to leverage the regex to match only to encntr, encntrprov, or encntrlocation by supplying those values in the regex.我想通过在正则表达式中提供这些值来利用正则表达式仅匹配 encntr、encntrprov 或 encntrlocation。 Thus encntr couldn't not match to encntrprov or encntrlocation using the regex.因此 encntr 无法使用正则表达式匹配 encntrprov 或 encntrlocation。

Here's the code snippet I'm trying to update.这是我正在尝试更新的代码片段。

public static FilenameIdentifierEnum IdentifyFile(string txtFilename)
{
    FilenameIdentifierEnum identifier = FilenameIdentifierEnum.unassigned;

    foreach (FilenameIdentifierEnum possibleIdentifier in Enum.GetValues(typeof(FilenameIdentifierEnum)))
    {
        // I would like this Regex.Match to eliminate the need to have the inner if statements 
        //if(Regex.IsMatch(txtFilename, $"(?i){possibleIdentifier.ToString()}(?-i)", RegexOptions.IgnoreCase))
        if (txtFilename.Contains(possibleIdentifierLower, StringComparison.OrdinalIgnoreCase))
        {
            identifier = possibleIdentifier;

            if (identifier == FilenameIdentifierEnum.encntr)
                identifier = EncntrCaseChecking(txtFilename, identifier);
            if (identifier == FilenameIdentifierEnum.appt)
                identifier = ApptCaseChecking(txtFilename, identifier);

            break;
        }
    }

    if (identifier == FilenameIdentifierEnum.unassigned)
    {
        throw new UnknownFileException($"Unknown identifier in filename or no file identifier in filename found. Text Filename: {txtFilename}");
    }

    return identifier;
}

private static FilenameIdentifierEnum EncntrCaseChecking(string txtFilename, FilenameIdentifierEnum possibleIdentifier)
{
    possibleIdentifier = (txtFilename.Contains(FilenameIdentifierEnum.encntrloc.ToString(), StringComparison.OrdinalIgnoreCase)
        ? FilenameIdentifierEnum.encntrloc
        : possibleIdentifier);

    possibleIdentifier = (txtFilename.Contains(FilenameIdentifierEnum.encntrprov.ToString(), StringComparison.OrdinalIgnoreCase)
        ? FilenameIdentifierEnum.encntrprov
        : possibleIdentifier);

    return possibleIdentifier;
}

private static FilenameIdentifierEnum ApptCaseChecking(string txtFilenameLower, FilenameIdentifierEnum possibleIdentifier)
{
    return (txtFilenameLower.Contains(FilenameIdentifierEnum.apptpart.ToString(), StringComparison.OrdinalIgnoreCase)
         ? FilenameIdentifierEnum.apptpart
         : possibleIdentifier);
}

public enum FilenameIdentifierEnum
{
    unassigned,
    encntr,
    encntrprov,
    encntrloc,
    persondemo,
    personbenefitcoverage,
    personprov,
    medication,
    immunization,
    allergy,
    diagnosis,
    problem,
    labresults,
    socialhistory,
    vitals,
    procedures,
    appt,
    apptpart,
    appointments,
    appointmentparticipant,
    encounterlocation,
    result
}

Thank you for your help.感谢您的帮助。

Use this regular expression:使用这个正则表达式:

(encntr\..*?)

Matches with substring encntr.与子字符串encntr.匹配encntr. only.只要。

Note that ^ is not a (not) operator.请注意, ^不是(非)运算符。 ^ is for matching from beginning of a string. ^用于从字符串的开头进行匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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