正则表达式 – Powershell:从字符串中提取文本
如何从字符串中提取“程序名称”。字符串将如下所示:
程序名称为(SUB RAD MSD 50R III)。将结果存储在另一个字符串中是正确的。我正在学习powerhell,所以任何解释你的答案将不胜感激。
以下正则表达式在括号之间提取任何内容:
PS> $prog = [regex]::match($s,'(([^)]+))').Groups[1].Value PS> $prog SUB RAD MSD 50R III Exlanation (created with RegexBuddy) Match the character '(' literally ?(? Match the regular expression below and capture its match into backreference number 1 ?([^)]+)? Match any character that is NOT a ) character ?[^)]+? Between one and unlimited times,as many times as possible,giving back as needed (greedy) ?+? Match the character ')' literally ?)? 检查这些链接: http://www.regular-expressions.info http://powershell.com/cs/blogs/tobias/archive/2011/10/27/regular-expressions-are-your-friend-part-1.aspx http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular-expressions-are-your-friend-part-2.aspx http://powershell.com/cs/blogs/tobias/archive/2011/12/02/regular-expressions-are-your-friend-part-3.aspx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |