- [정규식] - String 인 날짜 관련 2023.10.12
- [JAVA] 문자열 정규식으로 자르기 2019.11.12
- [JAVA] Arrays 2019.11.05
2023. 10. 12. 15:07
이런 값을
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 |
2019. 11. 12. 11:23
public class Test
{
public static void main(String[] args)
{
//문자열 정규식으로 자르기 ,.ㅣ
String src = "이름:홍길동, Java:100, HTML:80, Script:85";
String[] result = src.split("[:,]");
//for each문
for(String resul: result)
{
System.out.println(resul.trim()); //trim() 공백을 없애는 메서드
}
}
}
'JAVA > 정규식' 카테고리의 다른 글
[정규식] - String 인 날짜 관련 (0) | 2023.10.12 |
---|---|
[JAVA] Arrays (0) | 2019.11.05 |
2019. 11. 5. 11:03
import java.util.Arrays;
public class Test {
public static void main(String[] args)
{
String[] datas = {"트랜스포머", "스파이더맨", "원더우먼", "킬링"};
Arrays.sort(datas, (o1,o2) -> {
if(o1.length() > o2.length())
{
return -1;
}
else
return 1;
});
System.out.println(Arrays.toString(datas));
}
}
'JAVA > 정규식' 카테고리의 다른 글
[정규식] - String 인 날짜 관련 (0) | 2023.10.12 |
---|---|
[JAVA] 문자열 정규식으로 자르기 (0) | 2019.11.12 |