正则表达式匹配任何字符串powershell [英] Regex Match any string powershell

查看:291
本文介绍了正则表达式匹配任何字符串powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我对正则表达式的感觉如何,它们似乎总是在击败我.

No matter how well I feel like I know regular expressions, they always seem to beat me.

我正在寻找一种可以匹配任何字符串的通用模式.我能弄清楚如何处理所有这些不同的命名约定的唯一方法是制作一堆不同的正则表达式模式,现在我什至不确定是否所有数据都已被提取,因此我将不得不手动进行交叉检查.它.

I am looking for a universal pattern that will match any string. The only way I could figure out how to handle all these different naming conventions, was make a bunch of different regex patterns, and now I'm not even sure if all the data is getting picked up so I would have to manually cross-check it.

我只是想拿起可能放在两个方括号[]中的所有内容:

I am just trying to pick up anything that could possibly be within two brackets [ ] :

elseif($line -match "\[\w*\d*\]") {         
    $pars = $matches[0]
}
elseif($line -match "\[\d*\w*\]") {
    $pars = $matches[0]
}
elseif($line -match "\[\w*\d*_\w*\]") {
    $pars = $matches[0]
}
elseif($line -match "\[\w*\d*_*\w*-*\w*:*\w*\]") {
    $pars = $matches[0]
}            
elseif($line -match "\[\w*_*\w*_*\w*_*\w*_*\w*_*\w*-*\w*\]") {
    $pars = $matches[0]
}

我的操作方式不会产生错误,但是我不确定它是否可以解决我可能遇到的所有情况.使用这么多的数据,手动检查几乎是不可能的.

The way I am doing it does not generate errors, but I am not sure it handles all the situations I could potentially come across. Checking manually is almost impossible with this much data.

此外,如果有人知道生成正则表达式模式的强大工具,将不胜感激.我只能找到对我来说不是很有用的正则表达式测试器,而且对于带有powershell的正则表达式,在线帮助不大.

Also, if anyone knows of a great utility for generating regex patterns it would be much appreciated. I have only been able to find regex testers which isn't very useful to me, and there is little help online for regular expressions with powershell.

推荐答案

$a = [regex]"\[(.*)\]"
$b = $a.Match("sdfqsfsf[fghfdghdfhg]dgsdfg") 
$b.Captures[0].value

这篇关于正则表达式匹配任何字符串powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆