Search code examples
c#regexregular-language

C# regular expression for 2018-09-24 22:42:02.071042000


Goal: need to extract the string "2018-09-24 22:42:02" from the string below:

-rw-rw-rw- 1 rats 38K 2018-09-24 22:42:02.071042000 +0000 /prod/data/automation_sent/20180924/11/20180924_1100600.sps

I tried the following reg expressions but they dont seem to work with my string above:

An unescaped delimiter must be escaped with a backslash ():

\d{4}(?:/\d{1,2}){2} 

changed to

\d{4}(?:\/\d{1,2}){2} 

and got the wrong string

\d{4}[- /.]([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])

no matches:

\d{4}$|^\d{4}-((0?\d)|(1[012]))-(((0?|[12])\d)|3[01]) 

not sure what I am missing... any help is welcomed,

Thank you


Solution

  • how about this?

            string input = @"-rw-rw-rw- 1 rats 38K 2018-09-24 22:42:02.071042000 +0000 /prod/data/automation_sent/20180";
            string regex = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
    
            var match = Regex.Match(input, regex);