none
How to ignore first line in Regex RRS feed

  • Question

  • I can't find the exact forum for the regex, hopefully this is ok to ask regex question here. 

    I’ve a following standard output,

     

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Parameters

        ProtCalibDeviceBS    REG_DWORD    0x10

        ProtCalibDeviceID    REG_DWORD    0x45

    ProtCalibDeviceSR    REG_DWORD    0xbb80

     

    If I use

            string regex = @"([\s\S]+?)REG_DWORD([\s\S]+?)";

                MatchCollection matches = Regex.Matches(standardOutput, regex);

    But the groups become "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Parameters ProtCalibDeviceBS    REG_DWORD"

    "0x10 ProtCalibDeviceID    REG_DWORD"

    "0x45 ProtCalibDeviceSR    REG_DWORD"

    Look like I can't simply use regex to achieve the following match collection (correct me if I'm wrong). What is the best way to ignore the first line highlighted above but have the matches to the following Match Collection group:-

     

    Groups[0]

    Groups[1]

    Groups[2]

    ProtCalibDeviceBS

    REG_DWORD   

    0x10

    ProtCalibDeviceID

    REG_DWORD   

    0x45

    ProtCalibDeviceSR

    REG_DWORD   

    0xbb80

     

    Your help is appreciated.

     

    Thanks.

    Wednesday, June 7, 2017 2:36 AM

Answers

  • Try this:

    string regex = @"^(.+?)\s+REG_DWORD\s+(.+)$";
    MatchCollection matches = Regex.Matches( standardOutput, regex, RegexOptions.Multiline );

    And you should watch the groups 1 and 2.

    It ignores the first line not because it is the first, but because it does not contain REG_DWORD. I hope that it is suitable.

    The expression can be improved to detect the names and the numbers more exactly.





    • Edited by Viorel_MVP Wednesday, June 7, 2017 6:12 AM
    • Marked as answer by Wood-MSDN Wednesday, June 7, 2017 6:37 AM
    Wednesday, June 7, 2017 6:07 AM

All replies

  • Try this:

    string regex = @"^(.+?)\s+REG_DWORD\s+(.+)$";
    MatchCollection matches = Regex.Matches( standardOutput, regex, RegexOptions.Multiline );

    And you should watch the groups 1 and 2.

    It ignores the first line not because it is the first, but because it does not contain REG_DWORD. I hope that it is suitable.

    The expression can be improved to detect the names and the numbers more exactly.





    • Edited by Viorel_MVP Wednesday, June 7, 2017 6:12 AM
    • Marked as answer by Wood-MSDN Wednesday, June 7, 2017 6:37 AM
    Wednesday, June 7, 2017 6:07 AM
  • Thanks a lot, this works. 
    Wednesday, June 7, 2017 6:37 AM
  • Hi Viorel, 

    Just curious to know what the purpose of '?' in regex. 

    string regex = @"^(.+?)\s+REG_DWORD\s+(.+)$";

    Look like without this '?', e.g. regex = @"^(.+)\s+REG_DWORD\s+(.+)$"

    I can still get the same match collection. I might have missed certain scenario, please advise. 

    Thanks. 

    Wednesday, June 7, 2017 8:00 PM
  • Consider the scenario when there are several spaces before REG_DWORD. Then Group 1 will include trailing spaces after the name (e.g. “ProtCalibDeviceBS  ”). You will have to trim them. With “.+?”, these unneeded spaces are not included.

    Wednesday, June 7, 2017 8:25 PM
  • Thanks. 
    Thursday, June 8, 2017 7:15 AM
  • Hi Viorel, 

    A side question, is it possible to match 2 unique lines by 1 regex? 

    e.g. 

        ProtCalibDeviceBS    REG_SZ   Test

        ProtCalibDeviceID    REG_QWORD   0x45

    ProtCalibDeviceSR    REG_DWORD    0xbb80

    Groups[1]

    Groups[2]

    Groups[3]

    Groups[4]

    ProtCalibDeviceBS

    Test

    ProtCalibDeviceSR  

    0xbb80

    Thanks in advance. 

    Thursday, June 8, 2017 7:36 AM