Regex to match a string after colon

后端 未结 4 1812
长情又很酷
长情又很酷 2020-12-03 12:36

Input string is something like this: OU=TEST:This001. We need extra \"This001\". Best in C#.

相关标签:
4条回答
  • 2020-12-03 13:13

    What about :

    /OU=.*?:(.*)/
    

    Here is how it works:

    OU=  // Must contain OU=
    .    // Any character
    *    // Repeated but not mandatory
    ?    // Ungreedy (lazy) (Don't try to match everything)
    :    // Match the colon
    (    // Start to capture a group
      .    // Any character
      *    // Repeated but not mandatory
    )    // End of the group
    

    For the / they're delimiters to know where the regex start and where it ends (and for adding options).

    The captured group will contain This001.

    But it would be faster with a simple Substring().

    yourString.Substring(yourString.IndexOf(":")+1);
    

    Resources :

    • regular-expressions.info
    0 讨论(0)
  • 2020-12-03 13:13

    A solution without regex:

    var str = "OU=TEST:This00:1";
    var result = str.Split(new char[] { ':' }, 2)[1];
    
    // result == This00:1
    

    Regex vs Split vs IndexOf

    Split

    var str = "OU=TEST:This00:1";
    
    var sw = new Stopwatch();
    
    sw.Start();
    var result = str.Split(new char[] { ':' }, 2)[1];
    sw.Stop();
    
    // sw.ElapsedTicks == 15
    

    Regex

    var str = "OU=TEST:This00:1";
    
    var sw = new Stopwatch();
    
    sw.Start();
    var result = (new Regex(":(.*)", RegexOptions.Compiled)).Match(str).Groups[1];
    sw.Stop();
    
    // sw.ElapsedTicks == 7000 (Compiled)
    

    IndexOf

    var str = "OU=TEST:This00:1";
    
    var sw = new Stopwatch();
    
    sw.Start();
    var result = str.Substring(str.IndexOf(":") + 1);
    sw.Stop();
    
    // sw.ElapsedTicks == 40
    

    Winner: Split

    Links

    • Split
    • IndexOf
    • Regex
    0 讨论(0)
  • 2020-12-03 13:18

    "OU=" smells like you're doing an Active Directory or LDAP search and responding to the results. While regex is a brilliant tool, I just wanted to make sure that you're also aware of the excellent System.DirectoryServices.Protocols classes that were made for parsing, filtering and manipulating just this sort of data.

    The SearchResult, SearchResultEntry and DirectoryAttribute in particular would be the friends you might be looking for. I don't doubt that you can regex or substring as cleverly as the next guy but it's also nice to have another good tool in the toolbox.

    Have you tried these classes?

    0 讨论(0)
  • 2020-12-03 13:38

    if the OU=TEST: is your requirement before the string you want to match, use this regex:

    (?<=OU\s*=\s*TEST\s*:\s*).*
    

    that regex matches any length of text after the colon, whereas any text before the colon is just a requirement.

    You can replace TEST with [A-Za-z]+ to match any text other than TEST, or you can replace TEST with [\w]+ to match any length of any combination of alphabet and numbers.

    \s* means it might be any number of whitespaces or nothing in that position, remove it if you don't need such a check.

    0 讨论(0)
提交回复
热议问题