I have following strings :
"envelopeId": "fsadjhfkhdsgf", "ashgjkadsagagsalkjghsag": "gsajgks", "envelopeId": "fsadjhfkhdsgfgsd", "afshdj":"gas"
And I need to extract out all envelopeIds
from this. I have tried different regex
but nothing seems to work.
(?:"envelopeId": ")*(",)
(?:"envelopeId": ")*[\d\D](",)
What is wrong with the regex
I am trying?
You may use
"envelopeId":\s*"([^"]+)"
And extract matcher.group(1)
values. See the regex demo.
Details
"envelopeId":
- a literal string\s*
- 0+ whitespaces"
- a "
char([^"]+)
- Group 1: any 1+ chars other than "
"
- a "
char.String s = "\"envelopeId\": \"fsadjhfkhdsgf\", \"ashgjkadsagagsalkjghsag\": \"gsajgks\", \"envelopeId\": \"fsadjhfkhdsgfgsd\", \"afshdj\":\"gas\"";
String key = "envelopeId";
Pattern pattern = Pattern.compile("\"" + Pattern.quote(key) + "\":\\s*\"([^\"]+)\"");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(1));
}
Output:
fsadjhfkhdsgf
fsadjhfkhdsgfgsd
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加