none
Regular Expression Help RRS feed

  • Question

  • Hi,

    i have the following text in a file:

    ['06:00:00','07:00:00']

    i want to end up with this:

    '06:00:00 07:00:00' 

    so if there are quoted fields surrounded by brackets and also have a comma separating them, i want both fields combined into 1 quoted field without the comma.

    i tried a bunch of different regex commands, but could not come up with a solution.

    thanks

    Friday, February 21, 2020 3:58 PM

Answers

  • I was able to use the following regex expression:

    (\['(\d\d:\d\d:\d\d)','(\d\d:\d\d:\d\d)'\])

    and this gave me what i needed.

    thanks

    • Marked as answer by ScottCabral Tuesday, February 25, 2020 2:42 PM
    Tuesday, February 25, 2020 2:42 PM

All replies

  • you could do this without regular expressions:
    string test = "['06:00:00','07:00:00']";
    string result = test.Trim('[',']').Replace("','", " ");
    Friday, February 21, 2020 5:23 PM
  • Did you try the next method too?

    string example = "some text ['06:00:00','07:00:00'] ";
    
    string pattern = @"\['(\d\d:\d\d:\d\d)','(\d\d:\d\d:\d\d)'\]";
    Match m = Regex.Match( example, pattern );
    
    string result = $"'{m.Groups[1].Value} {m.Groups[2].Value}'";
    

    Friday, February 21, 2020 5:30 PM
  • thanks Viorel,

    the only thing is that gets me

    ['06:00:00','07:00:00']

    is there a way to remove the brackets?

    Friday, February 21, 2020 5:40 PM
  • The result variable in above sample should not contain “[ ]”. Show your example that reproduces the problem.


    • Edited by Viorel_MVP Friday, February 21, 2020 6:00 PM
    Friday, February 21, 2020 6:00 PM
  • hi

    IF it is in c# then why do not use replace functions

    string test = "['06:00:00','07:00:00']";
    string result = test.Trim().replace("[","").replace("]","").replace(",","");

    Saturday, February 22, 2020 6:58 AM
  • Hi ScottCabral
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 24, 2020 9:33 AM
  • I was able to use the following regex expression:

    (\['(\d\d:\d\d:\d\d)','(\d\d:\d\d:\d\d)'\])

    and this gave me what i needed.

    thanks

    • Marked as answer by ScottCabral Tuesday, February 25, 2020 2:42 PM
    Tuesday, February 25, 2020 2:42 PM