如何使用正则表达式获取文本块(PowerShell / C#)

时间:2018-01-02 18:48:48

标签: c# asp.net regex powershell

resource "aws_instance" "launch-ec2-servers"
{
    ami = "${var.ec2_ami}"
    instance_type = "${var.instance_type}"
    key_name = "${var.key_name}"
    vpc_security_group_ids = ["${var.security_group}"]
    subnet_id = "${var.subnet_id}"
    count= "${var.count}"

    root_block_device
    {
        volume_size = "${var.root_volume_size}"
        volume_type = "${var.root_volume_type}"
    }

    ebs_block_device
    {
        device_name = "${var.ebs_device_name}"
        snapshot_id = "${var.ebs_snapshot_id}"
        volume_type = "${var.ebs_volume_type}"
    }

    tags
    {
        Name = "${var.app_name}"
    }
}

嗨,

我正在尝试使用C#/ PowerShell / ASP.Net中的正则表达式获取 ebs_block_device 块部分。

到目前为止,我尝试过这个作为我的正则表达式: (ebs_block_device)(αS)([^}] +。)

我对代码感兴趣的块部分是:

 ebs_block_device
    {
       device_name = "${var.ebs_device_name}"
       snapshot_id = "${var.ebs_snapshot_id}"
       volume_type = "${var.ebs_volume_type}"
    }

你能帮我正确地抓住大括号之间的那段文字吗?

1 个答案:

答案 0 :(得分:0)

试试这个正则表达式:

(ebs_block_device)(\s+)(\{[\s\S]*?\}(?!"))