Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式和lookahead从字符串中提取版本_Regex_Powershell - Fatal编程技术网

Regex 使用正则表达式和lookahead从字符串中提取版本

Regex 使用正则表达式和lookahead从字符串中提取版本,regex,powershell,Regex,Powershell,我以下面的字符串为例,预计将来版本号会更改,并且可能会添加订单更改或其他类型) 我想用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, ".*(

我以下面的字符串为例,预计将来版本号会更改,并且可能会添加订单更改或其他类型)

我想用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)时,我尝试:

但这不匹配,顺序不正确吗?因此,我正在寻找正确的正则表达式,并给出一个小的解释

$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
编辑:我已修改正则表达式以匹配,无论顺序如何

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.

命名组的一些改进:

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

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

命名组的一些改进:

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

if ($subject -cmatch '((?:\d\.?)+) ((?=\(Plugin-based browsers\))|(?=\(Internet Explorer\)))') {
   $result = $matches[1]
} else {
   $result = ''
}
原因
(?:\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
反向示例顺序:

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
原因
(?:\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
反向示例顺序:

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

如果您不确定它们的顺序:

$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)。捕获一叠数字和点,并用该捕获替换整个字符串。使用文本字符串(基于插件的浏览器)重复上述操作。

如果您不确定它们的顺序:

$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)。捕获一叠数字和点,并用该捕获替换整个字符串。使用文本字符串(基于插件的浏览器)重复此操作。

尝试以下操作:

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

测试它。

试试这个:

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


测试它。

但我永远不确定顺序,这个字符串将来可能会更改(将编辑问题以使其更清楚)@Remko:我更新了我的答案,因此现在当元素的顺序颠倒时它会匹配。但我永远不确定顺序,这个字符串将来可能会更改(将编辑问题以使其更清楚)@Remko:我更新了我的答案,所以它现在在元素顺序颠倒时匹配。为什么不
([\d.])+(?=\(Internet\040Explorer\)”
?@devnull可能是因为我对正则表达式理解不够:d也许你可以添加这个作为答案,并解释为什么它不是
([\d\.]+)
,而不是
([\d\.])+
表达式
(?:\w+[\d\.]+(?=\(Internet\040Explorer\)
的唯一问题是不需要
(?:\w+
,而
[\d\.]+
part不考虑版本号后面的空格。Remko-我需要检测您那里的确切flash版本:12.0.0.38-很好奇您是如何推导出来的。我对DetectFlashVer的使用没有提供正确的检测。。您是如何检测到它的?为什么不
([\d.])+(?=\(Internet\040Explorer\)”
?@devnull可能是因为我对正则表达式理解不够:d也许您可以将此作为一个答案,并解释为什么它不是
([\d\.]+)
而不是
([\d\.])+
表达式唯一的问题
(?:\w+[\d.])(=\(Internet\040Explorer\)
是指不需要
(?:\w+)
,而
[\d\.]+
part没有考虑版本号后面的空格。Remko-我需要检测你那里的确切flash版本:12.0.0.38-很好奇你是如何推导出来的。我使用DetectFlashVer没有给我正确的检测。你是如何检测的?返回
12.0.0.38(Internet Explorer)
12.0.0.43(基于插件的浏览器)
(但我喜欢它的可读性)。要使它只返回版本
(?:
必须更改为
(?=
返回
12.0.0.38(Internet Explorer)
12.0.0.43(基于插件的浏览器)
(但我喜欢它的可读性)。要使其仅返回版本
(?:
必须更改为
(?=