Powershell 正则表达式检查括号之间的字符串 [英] Powershell Regex check for string between brackets

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

问题描述

我正在尝试将文本文件中的值读取到哈希表中,我希望能够知道何时遇到格式为['somestring']"的值......所以当我遇到一个带有括号的值我想将字符串存储到特定变量中并使用该字符串运行一个函数.

I'm trying to read values from a text file into a hashtable, I want to be able to be able to tell when I encounter a value that has the format "['somestring']"... So when I encounter a value that has brackets around it I want to store the string into a specific variable and run a function with that string.

我认为正则表达式是实现此目的的方法,但我不确定可能的正则表达式值是什么样的.任何帮助将不胜感激谢谢!

I was thinking that regex was the way to go for this but I am unsure what a possible regex value would look like. Any help would be appreciated thank you!

推荐答案

$r = [regex] "\[([^\[]*)\]"
$match = $r.match("[somestring]")
$text = $match.groups[1].value

这篇关于Powershell 正则表达式检查括号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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