使用Regular Expression和lookahead从字符串中提取版本

时间:2014-01-20 13:36:37

标签: regex powershell

我有以下字符串作为示例,预计版本号将来会发生变化,并且可能会添加订单更改或其他类型)

Flash Player 12.0.0.38 (Internet Explorer); 12.0.0.43 (Plugin-based browsers)

我想用以下方式用RegEx解析它:

  • 查找(Internet Explorer)并捕获其前面的版本(12.0.0.38)
  • 查找(基于插件的浏览器)并捕获其前面的版本

到目前为止我的尝试:

$text = "Flash Player 12.0.0.38 (Internet Explorer); 12.0.0.43 (Plugin-based browsers)"
[RegEx]::Match($text, ".*(?=\(Internet\040Explorer\))").Value

返回Flash Player 12.0.0.38

所以我认为我需要过滤“一个或多个单词”而不是捕获它们,然后捕获“一个或多个数字或”。当我跟着(Internet Explorer)我试过:

[RegEx]::Match($text, "(?:\w+)[\d\.]+(?=\(Internet\040Explorer\))").Value

但是那不匹配,订单是不正确的?所以我正在寻找正确的正则表达式,并附上一个小解释。

5 个答案:

答案 0 :(得分:1)

$text = "Flash Player 12.0.0.38 (Internet Explorer); 12.0.0.43 (Plugin-based browsers)"
$Regex = '[\d\.]+';
$Matches = [Regex]::Matches($text, $Regex);

$Matches[0].Value;
$Matches[1].Value;

结果如下:

12.0.0.38
12.0.0.43

编辑:我修改了正则表达式以匹配,无论顺序如何。

Clear-Host;

$matches = $null;
$Regex = '(?<=Flash Player\s)(?<FlashIE>[\d\.]+)(?:.*?)(?<FlashPlugin>[\d\.]+)(?=\s\(Plu)|(?<FlashPlugin>[\d\.]+)(?=\s\(Plu)(?:.*?)(?<=Flash Player\s)(?<FlashIE>[\d\.]+)';

# 1. Example string
$text = "Flash Player 12.0.0.38 (Internet Explorer); 12.0.0.43 (Plugin-based browsers)"
$MatchList = [Regex]::Matches($text, $Regex);
$MatchList[0].Groups['FlashIE'].Value;
$MatchList[0].Groups['FlashPlugin'].Value;

# 2. Reversed example string
$text = "; 12.0.0.43 (Plugin-based browsers);Flash Player 12.0.0.38 (Internet Explorer)"
$MatchList = [Regex]::Matches($text, $Regex);
$MatchList[0].Groups['FlashIE'].Value;
$MatchList[0].Groups['FlashPlugin'].Value;

# NOTE: Both of these yield the exact, same output, because we are using named groups.

结果:

12.0.0.38
12.0.0.43
12.0.0.38
12.0.0.43

答案 1 :(得分:1)

命名组有一点改进:

if ($text -cmatch '(?<plugin>(?:\d\.?)+) (?=\(Plugin-based browsers\))|(?<internet>(?:\d\.?)+) (?=\(Internet Explorer\))') {
    $PluginBrowsers = $matches['plugin']
    $InternetExplorer = $matches['internet']
}

你可以试试这个(没有经过测试):

使用两个正则表达式:

if ($subject -cmatch '((?:\d\.?)+) (?=\(Internet Explorer\))') {
   $result = $matches[1]
} else {
   $result = ''
}

if ($subject -cmatch '((?:\d\.?)+) (?=\(Plugin-based browsers\))') {
   $result = $matches[1]
} else {
   $result = ''
}

或者只有一个:

if ($subject -cmatch '((?:\d\.?)+) ((?=\(Plugin-based browsers\))|(?=\(Internet Explorer\)))') {
   $result = $matches[1]
} else {
   $result = ''
}

答案 2 :(得分:1)

原因 (?:\w+)[\d\.]+(?=\(Internet\040Explorer\))不匹配是因为[\d\.]+(?=\(Internet\040Explorer\))部分不期望版本号与(Internet Explorer)之间的空格

无论顺序如何,此表达式都将捕获所需的两个值:

(?=^.*?([\d\.]+)(?=(?> *)(?:\(Internet\040Explorer\))))(?=^.*?([\d\.]+)(?=(?> *)(?:\(Plugin-based browsers\))))

在powershell中:

$text = "Flash Player 12.0.0.38 (Internet Explorer); 12.0.0.43 (Plugin-based browsers)"
$regex = "(?=^.*?([\d\.]+)(?=(?> *)(?:\(Internet\040Explorer\))))(?=^.*?([\d\.]+)(?=(?> *)(?:\(Plugin-based browsers\))))"
$matches  = [RegEx]::Match($text, $regex)
echo $Matches.Groups[1].value  #Outputs 12.0.0.38
echo $Matches.Groups[2].value  #Outputs 12.0.0.43

示例顺序:

Flash Player 12.0.0.38 (Internet Explorer); 12.0.0.43 (Plugin-based browsers)
[Match number 1]
Matched: '' at character 1
[Capture Group 1] '12.0.0.38' found at character 14
[Capture Group 2] '12.0.0.43' found at character 45

反向示例顺序:

12.0.0.43 (Plugin-based browsers); Flash Player 12.0.0.38 (Internet Explorer)
[Match number 1]
Matched: '' at character 1
[Capture Group 1] '12.0.0.38' found at character 49
[Capture Group 2] '12.0.0.43' found at character 1

答案 3 :(得分:1)

如果你不确定他们的订单是什么:

$text = "Flash Player 12.0.0.38 (Internet Explorer); 12.0.0.43 (Plugin-based browsers)"

$IE_Version = $text -replace '.+\s([0-9.]+)\s\(Internet Explorer\).*','$1'
$Plugin_Version = $text -replace '.+\s([0-9.]+)\s\(Plugin-based browsers\).*','$1'

$IE_Version
$Plugin_Version

12.0.0.38
12.0.0.43

简而言之,正则表达式逻辑是:

搜索字符串,直到找到一个空格,后跟一大堆数字和点,然后是另一个空格,然后是文字字符串(Internet Explorer)。捕获一堆数字和点,并用这个捕获替换整个字符串。重复文字字符串(基于插件的浏览器)。

答案 4 :(得分:1)

试试这个:

([0-9.]*)(?: (\(Internet Explorer\)|\(Plugin-based browsers\)))

Regular expression visualization

Debuggex Demo

测试here