Regex 在powershell正则表达式中匹配多个条件
在powershell中,我需要遍历并返回以Regex 在powershell正则表达式中匹配多个条件,regex,powershell,Regex,Powershell,在powershell中,我需要遍历并返回以puppet\u开头的所有唯一第一个元素(在第一个:之前)的列表,例如puppet\u customer和puppet\u EDC。如果可能,从puppet_列表中排除一些,如puppet_enterprise和puppet_metrics_collector和puppet_agent chocolatey pe_install::install pe_postgresql::lib::devel profile::agent_config puppe
puppet\u
开头的所有唯一第一个元素(在第一个:
之前)的列表,例如puppet\u customer
和puppet\u EDC
。如果可能,从puppet_列表中排除一些,如puppet_enterprise
和puppet_metrics_collector
和puppet_agent
chocolatey
pe_install::install
pe_postgresql::lib::devel
profile::agent_config
puppet_agent::install::remove_packages
puppet_customer::customer_shared::alerts
puppet_customer::email_shared::email
puppet_edcs::claims::grt_api
puppet_enterprise::certs::puppetdb_whitelist
puppet_ets::windows::defaults::audit_policy
puppet_metrics_collector
puppet_policyservicing::owbinet::file_cluster
puppet_printserver::print
puppet_product::avrt::avrt
puppet_quoting::aqw_dca::aqw
puppet_testing::ourtestfile
puppet_webpresence::lifelanes_webapp
role::customer::customer_shared
这是我希望它返回的列表
puppet_customer
puppet_edcs
puppet_ets
puppet_policyservicing
puppet_printserver
puppet_product
puppet_quoting
puppet_webpresence
那么:
puppet|(?!企业|度量|收集器|代理|测试)(*):
通过提供必要的正则表达式来解决您的问题,该正则表达式包含一个否定的前瞻断言((?!…)
)
这个答案向您展示了一种避免所需正则表达式复杂性的替代方法
PowerShell有许多灵活的cmdlet和字符串运算符,虽然通过管道链接它们可能不是最快的选项,但它允许概念上简单的解决方案:
$invoke.name -split '::' -like 'puppet_*' |
Sort-Object -Unique |
Where { $_ -notin 'puppet_enterprise', 'puppet_metrics_collector', 'puppet_agent'}
注意:以上假设在任何给定行上,:
-以puppet
开头的分隔标记仅作为该行上的第一个标记出现(如果有);如果这个假设不成立,就需要做更多的工作。您可以使用
'^puppet_(?!enterprise|metrics_collector|agent|testing)\w+'
看。它将在一行的开头匹配puppet
,该行后面不紧跟enterprise
、metrics\u collector
、agent
、testing
,然后将使用一个或多个字字符:
注意:您需要访问整个匹配值,而不是捕获组值,即在本例中,$\uMatches.value
如果要避免仅在“停止”字后面跟有::
的情况下进行匹配,请按如下所示调整模式:
^puppet_(?!(?:enterprise|metrics_collector|agent|testing)::)\w+
^^^ ^^^
看这个 试试这个:
$yourlist | where {$_ -like "puppet_*" -and $_ -notlike '*puppet_enterprise*' -and $_ -notlike '*puppet_metrics_collector*' -and $_ -notlike '*puppet_agent*' } |
%{($_ -split "::")[0]} | sort -Unique
@阿布拉哈夫,请维克托回答。
$yourlist | where {$_ -like "puppet_*" -and $_ -notlike '*puppet_enterprise*' -and $_ -notlike '*puppet_metrics_collector*' -and $_ -notlike '*puppet_agent*' } |
%{($_ -split "::")[0]} | sort -Unique