이런 값을

List 일 경우, \ 가 붙는 데이터이고, 단일 데이터의 경우에는 \ 가 없다.

어떤게 올지 모르고, 둘 중 하나가 온다고 했을 때

2023.02.27(종일) ~ 2023.02.27(종일) \ 2023.02.29(종일) ~ 2023.02.29(종일)
2023.02.27(종일) ~ 2023.02.27(종일)

이렇게 바꾸려면?

String 2023.02.27(종일)
List<String> 2023.02.27(종일), 2023.02.29(종일)
/**
 * 여러 일정을 함께 등록한 경우 "/" 로 구분되는 것을 나눈다
 */
String[] durations = duration.split("/");

/**
 * 나눈 결과 N 개를 List<String> 형태로 변환한다.
 */
List<String> durationslist = Arrays.stream(durations).collect(Collectors.toList());

/**
 * durationslist index : 2023.02.27(종일) ~ 2023.02.27(종일)\
 * result : 2023.02.27(종일)
 */
String regEx = "([0-9]{4}\\.[0-9]{2}\\.[0-9]{2})\\(([가-힣]{2})\\)[ \\t\\n\\x0B\\f\\r]~[ \\t\\n\\x0B\\f\\r]([0-9]{4}\\.[0-9]{2}\\.[0-9]{2})\\(([가-힣]{2})\\)";
Pattern pattern = Pattern.compile(regEx);

/**
 * Durations 객체에 값 할당
 */
List<Duration> list = new ArrayList<>();
durationslist.forEach(x -> {
    Matcher matcher = pattern.matcher(x.trim());
    Duration durationObj = null;
    while(matcher.find()){
        durationObj = new Duration();
        durationObj.setStartDate(matcher.group(1).concat(":"+ matcher.group(2)));
        durationObj.setEndDate(matcher.group(3).concat(":"+ matcher.group(4)));
    }
    list.add(durationObj);
});
return list;

 

 

'JAVA > 정규식' 카테고리의 다른 글

[JAVA] 문자열 정규식으로 자르기  (0) 2019.11.12
[JAVA] Arrays  (0) 2019.11.05