Search code examples
regexpcre

2 Regular Expressions to extract build no and version name


Im really struggling to understand regular expressions.

I have this string:

Windows SERVERMAIN 10.0.14393 Microsoft Windows Server 2016 Standard x64

I need to create two regular expressions.

The first one should return the part of the string after the second space and before the third space, so I am left with:

10.0.14393

The second one should return everything after the third space, so I am left with:

Microsoft Windows Server 2016 Standard x64

Is anyone able to help me with this, so far I have only been able to use:

\s+\w+\s(.*)

Which gives me:

SERVERMAIN 10.0.14393 Microsoft Windows Server 2016 Standard x64

Update 1 After the help from @rock321987, I've reviewed how I want to implement this.

I now have this string:

Microsoft Windows Server 2016 Datacenter x64 - 10.0.14393

Which I want to split into two groups:

Microsoft Windows Server 2016 Datacenter x64

10.0.14393

Solution

  • Regex 1

    ^.*?[ ]+.*?[ ]+(.*?)[ ]
    

    Regex 2

    ^.*?[ ]+.*?[ ]+.*?[ ]+(.*)$
    

    Regex 1 Breakdown

    ^ #Start of string
    .*?[ ]+ #Match till 1st space
    .*?[ ]+ #Match till 2nd space
    (.*?)[ ]+ #Capture the match after 2nd space till 3rd space
    

    Regex 2 Breakdown

    ^.*?[ ]+.*?[ ]+.*?[ ]+ #Explanation same as above. Match till 3rd space
    (.*)$ #Match everything after 3rd space till last
    

    EDIT: This can be done in single regex too if your tool allows

    ^.*?[ ]+.*?[ ]+(.*?)[ ]+(.*)$
    

    EDIT 1: If you want you can use \K too like

    ^.*?[ ]+.*?[ ]+\K([^ ]+)