Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 Powershell-从SQLPlus查询检索计数_Regex_Powershell - Fatal编程技术网

Regex Powershell-从SQLPlus查询检索计数

Regex Powershell-从SQLPlus查询检索计数,regex,powershell,Regex,Powershell,我正在使用powershell脚本,它利用SQLPlus查询oracle数据库 查询是:从表中选择count* powershell查询输出: SQL*Plus: Release 11.2.0.1.0 Production on Sat Feb 6 09:50:52 2016 Copyright (c) 1982, 2010, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition

我正在使用powershell脚本,它利用SQLPlus查询oracle数据库

查询是:从表中选择count*

powershell查询输出:

SQL*Plus: Release 11.2.0.1.0 Production on Sat Feb 6 09:50:52 2016

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options


  COUNT(*)
----------
    50

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
我目前正在使用下面的语句来检索计数,尽管它似乎没有捕获结果

if($file -match 'count (?<count>\d+)'){$count=[int32]$matches['count']}
任何帮助/指导都将不胜感激。
谢谢

我看到一个问题和一个潜在问题。后者是我怀疑$file是一个字符串数组。这对你是不利的,因为你正在寻找一个数字的基础上存在的文本上面几行

问题是您的正则表达式不是多行的,并且在您的示例中,数字与计数不在同一行。你的正则表达式只能匹配计数50。这不是它在显示的文本中的显示方式

两个确保这两点使$file成为一个字符串,然后运行一个多行正则表达式,该正则表达式匹配计数后遇到的第一个数字。保留命名的匹配逻辑,因为这样做很好

# This can most likely be moved to where this is read in.
$file = $file | Out-String

# try and match the regex. 
if($file -match "(?s)Count.*?(?<count>\d+)"){
    [int32]$Matches.Count
}

$file的类型是什么$file.GetType.Fullname。您的查询没有考虑此问题的多行性质。此外,数字没有在计数后立即显示,这就是它失败的原因。您好@Matt,再次感谢您的帮助!因此,$file是从一个.txt文件中读取的,该文件包含一个SQLPlus调用的结果。$file的类型是System.String$file=Get Content-path$filepath | out String该解决方案也在运行非常感谢!你的匹配字符串是correct@StephenSugumar我也这么想,因为我觉得我帮了你。然而,我在这篇文章中看不到这一点,所以我回答说忽略了这种可能性。