【问题标题】:Need to get Particular word using java Regex需要使用 java Regex 获取特定的单词
【发布时间】:2021-10-17 09:05:37
【问题描述】:

我想在 java 中使用正则表达式来获取一个特定的单词。谢谢

在下面的段落中,我需要找到网络接口名称

resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {
name = "nic_LinuxVMCent-nhi"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
ip_configuration {
name = "pubIP_LinuxVMCent-nhi"
subnet_id = azurerm_subnet.sub_wind12VM-PtN.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pubIP_LinuxVMCent-nhi.id
}
}
data "azurerm_snapshot" "snapLinuxVMCent-nhi" {
name = "CentOS76New-0"
resource_group_name = "SaaSworkloadsnaps"
}

预期结果例如:

nic_LinuxVMCent-nhi

【问题讨论】:

    标签: java regex terraform terraform-provider-azure


    【解决方案1】:

    这是一个多行的文本。但是,似乎有一条您可以使用正则表达式识别的行:

    resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {

    因此,正则表达式将是 ^resource "azurerm_network_interface" "([^"]+)" {$ - 请参阅 https://regexr.com/67ldb

    您可以使用Matcher.match 查看任何行是否与此表达式匹配,如果匹配,则matcher.group(1) 将是您要查找的值。

    【讨论】:

      【解决方案2】:

      您可以使用此正则表达式查找网络接口名称:

      (?<=resource \"azurerm_network_interface\" \").+(?=\" {)
      

      我使用前瞻来查找名称。 此外,这是 regex101 的链接: Link

      我不知道网络接口,所以, 此正则表达式解决方案特定于“azurerm_network_interface”。 如果您需要任何其他帮助,请在下方评论。 干杯:)

      【讨论】:

        猜你喜欢
        • 2020-01-10
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        • 2017-10-02
        • 2018-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多