Reputation: 47
I'm trying to get some RegEx working but fail slightly at my specific use case.
Given the following string for example
Device-1: P0_Node0_Channel0_Dimm0 size: 32 GB speed: 2133 MHz type: DDR4
I want to extract informations from this string preferably like that:
Device-1: P0_Node0_Channel0_Dimm0
size: 32 GB
speed: 2133 MHz
type: DDR4
So I tried a bit around and tested some expressions
(.*?):\s
Does work to some regard. Catches the first parameter name properly but after that messes up with the spaces.
:\s(.*?)\s\w*?:!?
Although this catches the empty space in the third parameter value, it only gives me the first and the third value. Also no parameter names.
Someone has an idea how I could achieve the expected behaviour?
Note: I'm doing this in Excel VBA, not sure if all functions are supported there.
Thanks
Upvotes: 1
Views: 2797
Reputation: 2151
This is an Autoit example:
#include <Array.au3>
Local $str = 'Device-1: P0_Node0_Channel0_Dimm0 size: 32 GB speed: 2133 MHz type: DDR4'
$r_A = StringRegExp($str, '([\w-]*\:)\s*(\w*\s|.*)', 3)
ConsoleWrite(_ArrayToString($r_A, @CR) & @crlf)
Upvotes: 0
Reputation: 626932
You may use
([^\s:]+):\s*(.*?)\s*(?=[^\s:]+:|$)
See the regex demo
Details
([^\s:]+)
- Group 1: one or more chars other than whitespace and :
:
- a colon\s*
- zero or more whitespaces(.*?)
- Group 2: any 0+ chars other than line break chars up to the first occurrence of...
\s*
- zero or more whitespaces that are followed with...(?=[^\s:]+:|$)
- one or more chars other than whitespace and :
or end of stringUpvotes: 2