공부내용 정리/자바

[Java] Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0

minsoku 2024. 1. 22. 13:01

문자열을 나누는 split 함수에서 구분자로 + 를 사용하면

(+ 뿐만 아니라, *, ^ 도 마찬가지)

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 

라는 예외가 발생한다.

 

이유는 정규표현식에서 사용되는 문자이기 때문인데 단순문자인 구분자로 사용하고 싶으면 escape  시켜야 한다.

` ` `

They are called quantifiers.

* 0 or more of the preceding expression

+ 1 or more of the preceding expression

from: https://stackoverflow.com/questions/8575281/regex-plus-vs-star-difference

 

Regex plus vs star difference?

What is the difference between: (.+?) and (.*?) when I use it in my php preg_match regex?

stackoverflow.com

` ` `

"[+] " , "\\+"

 

<escape 전>

 

<escape 후>