匹配Systeminfo | findstr“内存”输出与正则表达式

时间:2011-04-25 13:38:30

标签: regex windows-7

这是输出:

Total Physical Memory:     3,840 MB
Available Physical Memory:   889 MB
Virtual Memory: Max Size:  7,677 MB
Virtual Memory: Available: 4,533 MB
Virtual Memory: In Use:    3,144 MB

我想要获取:

Total Physical Memory
Available Physical Memory
Virtual Memory: Max Size 
Virtual Memory: Available
Virtual Memory: In Use

分开。

1 个答案:

答案 0 :(得分:1)

听起来你想要这样的东西:

/^[A-Za-z\s:]+(?=:\s*\d+(,\d{3})*\s[MK]B)/

您需要确保将正则表达式匹配器设置为将^解释为行的开头,而不是整个字符串。这通常在某个地方设置为选项。

根据您的正则表达式风格,您可能还必须逃避:中的[A-Za-z\s:]

编辑:以下是解释:

^             #The beginning of a line
[A-Za-z\s:]+  #Any number of letters, spaces, and/or `:` characters, but at least one.
(?=...)       #A positive lookahead assertion, because we want to check for the presence of the upcoming pattern but not include it in our match.
:\s*          #A colon followed by any amount of whitespace.
\d+(,\d{3})*  #A number in X,XXX,XXX format, with any number of ,XXX groups.
\s[MK]B       #A space, then either MB or KB.

希望这很清楚。如果您需要,here's some info on lookaround assertions