I am using the regex - [{]{[}][}] for the input {{id}} The output I expect is id . But I am getting {{id}} .
My java code is as below:
String string = "\"{\n" +
" \"id\": {{dsm_id}},\n" +
" \"title\": \"Automation Test\",\n" +
" \"code\": \"aut_test\",\n" +
" \"companyId\": 286,\n" +
" \"enabled\": true,\n" +
" \"enableAdvanceFilter\": false,\n" +
" \"deleteAfterScheduledTime\": false,\n" +
" \"enableDataStoreLog\": false\n" +
"}\"";
String patternString = "[{][{](.*)[}][}]";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(string);
List<String> allMatches = new ArrayList<String>();
while (matcher.find()) {
allMatches.add(matcher.group());
}
System.out.println(allMatches);
The only solution I need is having the right regex. I cannot use or don't want to use DTO or Json parser here.
You use patter group incorrectly. Try this one:
String patternString = "[{][{](?<id>.*)[}][}]";
// String patternString = "\\{\\{(?<id>[^}]+)}}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(string);
List<String> allMatches = new ArrayList<>();
while (matcher.find()) {
allMatches.add(matcher.group("id"));
}
In case you do not want to use name group
:
String patternString = "[{][{](.*)[}][}]";
//String patternString = "\\{\\{([^}]+)}}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(string);
List<String> allMatches = new ArrayList<>();
while (matcher.find()) {
allMatches.add(matcher.group(1));
}
But I recommend you use another approach: using eg Jackson to convert this JSON to Map<String, Object>
and retrieve required fields.
It will be better to convert it to object and then you can find whatever you need.
Create DTO object
public class DsmDto{
private String id;
private String title;
private String code;
private float companyId;
private boolean enabled;
private boolean enableAdvanceFilter;
private boolean deleteAfterScheduledTime;
private boolean enableDataStoreLog;
// Getter Methods
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public String getCode() {
return code;
}
public float getCompanyId() {
return companyId;
}
public boolean getEnabled() {
return enabled;
}
public boolean getEnableAdvanceFilter() {
return enableAdvanceFilter;
}
public boolean getDeleteAfterScheduledTime() {
return deleteAfterScheduledTime;
}
public boolean getEnableDataStoreLog() {
return enableDataStoreLog;
}
// Setter Methods
public void setId(String id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setCode(String code) {
this.code = code;
}
public void setCompanyId(float companyId) {
this.companyId = companyId;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setEnableAdvanceFilter(boolean enableAdvanceFilter) {
this.enableAdvanceFilter = enableAdvanceFilter;
}
public void setDeleteAfterScheduledTime(boolean deleteAfterScheduledTime) {
this.deleteAfterScheduledTime = deleteAfterScheduledTime;
}
public void setEnableDataStoreLog(boolean enableDataStoreLog) {
this.enableDataStoreLog = enableDataStoreLog;
}
}
Create converter class and convert method
public class DmsConverter{
ObjectMapper mapper = new ObjectMapper();
public static DsmDto jsonToDsm(String json){
return mapper.readValue(json, DsmDto.class);
}
}
Usage:
DsmDto dsmDto = DsmConverter.jsonToDsm(json);
System.out.println(dsmDto.getId());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.