如何使用批处理脚本在两个模式之间提取字符串?

时间:2019-11-18 12:09:24

标签: batch-file

我在pom.xml文件中有以下一行。

<abc>xyz</abc>

我需要通过使用与sed -e's /(。*)/ \ 1 /'相同的批处理脚本进行模式匹配来提取xyz

,输出应为xyz。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以将<>用作delims。

@for /f "tokens=2delims=><  " %%i in ('type pom.xml') dodo set "result=%%i" & goto :show
:show
echo %result%

,或者仅基于标记的属性名称来获取特定值。

@for /f "tokens=2delims=><  " %%i in ('type pom.xml ^| find /I "abc"') do set "result=%%i" & goto :show
:show
echo %result%

答案 1 :(得分:0)

您可以尝试使用xpath.bat

// ./protected/models/FormBuilder.php

/**
 * FormBuilder AR model class with dynamic field building capabilities
 *
 * @property integer $id
 * @property string $form_title
 * @property string $form_fields Form fields definitions stored as JSON String
 */
class FormBuilder extends CActiveRecord {

    public function init_form_model() {
        $dynamic_fields = CJSON::decode($this->form_fields);
        foreach($dynamic_fields as $field => $props) {
            // Neither this
            $this->$field = '';
            // nor this working!!!
            $this->setAttribute($field, '');
        }
    }

    /* other methods as it is */
}

// ./protected/controllers/FormBuilderController.php
class FormBuilderController extends CController {
    // ...
    public function actionServing($form_id) {
        $form = FormBuilder::models()->findByPk($form_id);
        $form->init_form_model();
        $this->render('dyn_frm', ['form' => $form]);
    }
    // ...
}

将其保存到变量中:

FormBuilder->init_form_model()