加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 如何在powershell中编写正则表达式

发布时间:2020-12-14 05:56:19 所属栏目:百科 来源:网络整理
导读:我需要在power shell中使用正则表达式来按字符串##拆分字符串,并将字符串移除到另一个字符(;). 我有以下字符串. $temp = "admin@test.com## deliver,expand;user1@test.com## deliver,expand;group1@test.com## deliver,expand;" 现在,我想拆分这个字符串,只
我需要在power shell中使用正则表达式来按字符串##拆分字符串,并将字符串移除到另一个字符(;).

我有以下字符串.

$temp = "admin@test.com## deliver,expand;user1@test.com## deliver,expand;group1@test.com## deliver,expand;"

现在,我想拆分这个字符串,只获得电子邮件ID到新的数组对象.我的预期输出应该是这样的.

admin@test.com
user1@test.com
group1@test.com

为了得到上面的输出,我需要用字符##分割字符串并删除子字符串到分号(;).

任何人都可以帮我写一个正则表达式查询来实现PowerShell中的这种需求吗?

解决方法

如果要在方法中使用基于正则表达式的拆分,可以使用## [^;] *;正则表达式和此代码也将删除所有空值(使用|?{$_}):

$res = [regex]::Split($temp,'##[^;]*;') | ? { $_ }

## [^;] *;火柴:

> ## – 双#
> [^;] * – 除零之外的零个或多个字符;
>; – 文字;

见regex demo

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读