在两个单词之间提取字符串

时间:2017-04-13 12:52:04

标签: regex powershell

我尝试了以下操作来获取当前目录中每个文件的translator.t('')之间的所有字符串:

get-childitem . -include *.ts -rec | select-string -pattern "(?<=translator.t\(\')(.*)(?=\'\))"

它没有用,它放在shell上(例如)

app\services\fxutils.service.ts:855:                return this.translator.t('setup_flags');
app\services\fxutils.service.ts:857:                return this.translator.t('zone1_setup');
app\services\fxutils.service.ts:859:                return this.translator.t('zone2_setup');
app\services\fxutils.service.ts:892:                        return this.translator.t('0');
app\services\fxutils.service.ts:894:                        return this.translator.t('1');
app\services\fxutils.service.ts:904:                        return this.translator.t('scenario') + ' ' + v
app\services\fxutils.service.ts:908:                        return this.translator.t('sequence') + ' A';
app\services\fxutils.service.ts:910:                        return this.translator.t('sequence') + ' B';
app\services\fxutils.service.ts:918:                        return this.translator.t('scenario') + ' ' + v
app\services\fxutils.service.ts:922:                        return this.translator.t('sequence') + ' A';
app\services\fxutils.service.ts:924:                        return this.translator.t('sequence') + ' B';
app\services\fxutils.service.ts:931:                        return this.translator.t('-1');
app\services\fxutils.service.ts:941:                        tmp.vlm = this.translator.t('-1');
app\services\fxutils.service.ts:946:                        tmp.vlt = this.translator.t('-1');
app\services\fxutils.service.ts:1017:                return this.translator.t('scenario') + ' ' + num;
app\services\fxutils.service.ts:1025:            else if (val.vlm == 101) return this.translator.t('reset_
app\services\fxutils.service.ts:1026:            else if (val.vlm == 102) return this.translator.t('reset_
app\services\fxutils.service.ts:1027:            else return this.translator.t('no_action');
app\services\fxutils.service.ts:1035:            else if (val.vlm == 101) vlm = this.translator.t('reset_u
app\services\fxutils.service.ts:1036:            else if (val.vlm == 102) vlm = this.translator.t('reset_d
app\services\fxutils.service.ts:1037:            else vlm = this.translator.t('no_action');
app\services\fxutils.service.ts:1040:            else vlt = this.translator.t('no_action');
app\services\fxutils.service.ts:1046:            else if (num == 0) return this.translator.t('cmd_disarm')
app\services\fxutils.service.ts:1047:            else if (num == 1) return this.translator.t('cmd_arm');
app\services\fxutils.service.ts:1052:            else if (num == 1) return this.translator.t('t1');
app\services\fxutils.service.ts:1053:            else if (num == 2) return this.translator.t('t2');
app\services\fxutils.service.ts:1054:            else if (num == 3) return this.translator.t('t3');
app\services\fxutils.service.ts:1057:            if (num == -1) return this.translator.t('no_action');
app\services\fxutils.service.ts:1061:            if (num == -1) return this.translator.t('no_action');
app\services\fxutils.service.ts:1062:            else if (num == 1) return this.translator.t('disable');

但我想只匹配并在shell中输入以下单词:setup_flags,zone1_setup等...

我怎么能得到这个?

1 个答案:

答案 0 :(得分:1)

您必须访问从Matches cmdlet返回的Select-String属性:

get-childitem . -include *.ts -rec  |
    select-string -pattern "(?<=translator.t\(\')(.*)(?=\'\))" | 
    ForEach-Object {
        $_.Matches.Groups[1].value
    }