You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何从指定格式的resourceId字符串提取目标子串?

在Java中提取Azure资源ID的特定子串方法

输入字符串示例:

"resourceId=/subscriptions/subscription_id/resourceGroups/rg_name/providers/Microsoft.Compute/virtualMachines/resource_name/i=xxxxxxxxx/y=2021/m=03/d=13/h=15/m=00/PT1H.json"

目标提取子串:

/subscriptions/subscription_id/resourceGroups/rg_name/providers/Microsoft.Compute/virtualMachines/resource_name

需求说明:提取从开头到virtualMachines后紧跟的资源名的完整子串,资源名可能是vm_name、akv_name等任意名称,后续路径部分需要截断。


方法一:使用substring结合indexOf实现

思路

  1. 先剥离字符串开头的resourceId=前缀;
  2. 定位virtualMachines/的位置,找到它之后第一个/的索引,这个索引就是截断的终点;
  3. 通过substring提取目标区间的子串,同时兼容资源名是路径最后一段的场景(即后面没有/的情况)。

代码示例

public class ResourceIdExtractor {
    public static void main(String[] args) {
        String resourceId = "resourceId=/subscriptions/subscription_id/resourceGroups/rg_name/providers/Microsoft.Compute/virtualMachines/resource_name/i=xxxxxxxxx/y=2021/m=03/d=13/h=15/m=00/PT1H.json";
        
        // 移除开头的"resourceId="前缀
        String idWithoutPrefix = resourceId.replaceFirst("^resourceId=", "");
        
        // 标记要定位的路径段
        String marker = "virtualMachines/";
        int markerIndex = idWithoutPrefix.indexOf(marker);
        
        if (markerIndex != -1) {
            // 找到标记后第一个"/"的位置
            int nextSlashIndex = idWithoutPrefix.indexOf("/", markerIndex + marker.length());
            
            if (nextSlashIndex != -1) {
                // 提取到目标子串
                String targetPath = idWithoutPrefix.substring(0, nextSlashIndex);
                System.out.println(targetPath);
            } else {
                // 如果资源名是最后一段路径,直接返回完整前缀后的字符串
                System.out.println(idWithoutPrefix);
            }
        } else {
            System.out.println("输入字符串不包含virtualMachines路径段");
        }
    }
}

方法二:使用正则表达式实现

思路

构造正则表达式匹配固定格式的资源ID,直接捕获从/subscriptions/开始,到virtualMachines/后第一个/之前的完整路径,这种方式代码更简洁,适合格式固定的场景。

代码示例

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResourceIdRegexExtractor {
    public static void main(String[] args) {
        String resourceId = "resourceId=/subscriptions/subscription_id/resourceGroups/rg_name/providers/Microsoft.Compute/virtualMachines/resource_name/i=xxxxxxxxx/y=2021/m=03/d=13/h=15/m=00/PT1H.json";
        
        // 正则规则:匹配resourceId前缀,捕获目标路径段
        String regex = "^resourceId=(/subscriptions/.+/virtualMachines/[^/]+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(resourceId);
        
        if (matcher.find()) {
            String targetPath = matcher.group(1);
            System.out.println(targetPath);
        } else {
            System.out.println("未匹配到符合要求的路径");
        }
    }
}

正则规则说明

  • ^resourceId=:匹配字符串开头的固定前缀;
  • (/subscriptions/.+/virtualMachines/[^/]+):捕获核心目标路径:
    • /subscriptions/.+:匹配从订阅根路径到virtualMachines之前的所有内容;
    • /virtualMachines/:精确匹配路径段标记;
    • [^/]+:匹配任意非/的字符(即资源名,直到下一个路径分隔符为止)。

内容的提问来源于stack exchange,提问作者dev007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:37:23