从Java字符串中以所需格式提取子字符串?

huangapple go评论66阅读模式
英文:

How to fetch substring in required format from a String in java?

问题

"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"

我正在尝试按以下方式提取子字符串,理想情况下是根据"/"拆分字符串,并提取字符串的第一部分,直到resource_name:

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

如何在Java中使用substring或正则表达式来实现这种格式?

编辑

在给定的字符串中,resource_name将被替换为vm_name或akv_name。它不会永远是字符串中的resource_name,我已经给出了通用名称。

基本上,我需要从开头一直到virtualMachines,并获取附加在virtualMachines后面的resource_name,后面跟着一个斜杠。

在这种情况下,如何处理它?

英文:

I have the String in the below format.

"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"

I'm trying to fetch the substring in the below manner by ideally splitting the string based on "/" and fetch the first part of string until resource_name :

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

How to achieve this format either by using substring or regEx in java?

Edited

In the given string ,resource_name will be substituted with vm_name or akv_name. It will be not be resource_name for ever in the string, have given generic name.

Basically, I need to go until virtualMachines from the start and fetch the resource_name which is appended next to virtualMachines by a slash

Then in this scenario.. how to handle it?

答案1

得分: 2

以下是翻译好的部分:

有几种方法可以做到这一点。

您可以使用 PatternMatcher,使用正则表达式捕获来隔离数据。

或者,您可以使用 replace 过程来删除不需要的数据。

最后,您可以使用 indexOf 过程来获取 substring 方法的结束索引。

因为简单性,我推荐最后一种选项。

考虑以下方法 parse,它将 String 值作为第一个参数,并将起始和结束的 String 值作为第二和第三个参数。

String parse(String string, String start, String end) {
    int indexOf = string.indexOf(start);
    return string.substring(indexOf, string.indexOf(end) + end.length());
}

这是它的使用示例。

String string = "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";
String value = parse(string, "subscriptions", "resource_name");
System.out.println(value);

输出

subscriptions/subscription_id/resourceGroups/rg_name/providers/Microsoft.Compute/virtualMachines/resource_name
英文:

There are a few ways to do this.

You can use a Pattern and Matcher, to isolate the data using a regular expression capture.

Or, you can use a replace procedure to remove the data you don't want.

Or, finally, you can use an indexOf procedure to acquire an end-index for a substring method.

Because of the simplicity, I recommend the final option.

Consider the following method, parse, which takes the String value as the first parameter, and the starting and ending String values as the second and third parameter.

String parse(String string, String start, String end) {
    int indexOf = string.indexOf(start);
    return string.substring(indexOf, string.indexOf(end) + end.length());
}

And, here is an example of it's usage.

String string = "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";
String value = parse(string, "subscriptions", "resource_name");
System.out.println(value);

Output

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

答案2

得分: 1

使用indexOf的解决方案:

String input = "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";

// 找到"resource_name"的索引
int endIndex = input.indexOf("/resource_name");

// 提取子字符串直到"resource_name"
String substring = input.substring(0, endIndex);

System.out.println(substring);

使用replaceAll的解决方案:

String input = "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";

String regex = "/resource_name.*$";
String substring = input.replaceAll(regex, "");

System.out.println(substring);

在我的端口,它运行良好。希望这些对您有帮助。

英文:

I have two solutions.
using indexOf:

String input = "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";

// Find the index of "resource_name"
int endIndex = input.indexOf("/resource_name");

// Extract the substring until "resource_name"
String substring = input.substring(0, endIndex);

System.out.println(substring);

using replaceAll:

String input = "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";

String regex = "/resource_name.*$";
String substring = input.replaceAll(regex, "");

System.out.println(substring);

In my end, it works well. Hope these would give you help.

答案3

得分: 0

String sample = "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";
StringBuilder subString = new StringBuilder(sample.substring(11, sample.indexOf("/i=xxxxxxxxx")));
String endString = sample.substring(sample.indexOf("/i=xxxxxxxxx"));
int lastIndex = subString.lastIndexOf("/");
new StringBuilder(subString.replace(lastIndex, subString.length(), "/vm_name").append(endString));
英文:

You can do something like this,

 String sample = "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";
 StringBuilder subString = new StringBuilder(sample.substring(11, sample.indexOf("/i=xxxxxxxxx"));
 String endString = sample.substring(sample.indexOf("/i=xxxxxxxxx"));
 int lastIndex=subString.lastIndexOf("/");
 new StringBuilder(subString.replace(lastIndex,subString.length(),"/vm_name").append(endString));

huangapple
  • 本文由 发表于 2023年5月22日 07:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定