문제

Is there a library or any easy way to convert a string and make sure its compatible as a regex to look for and replace in another string. So if the string is "$money" it would get converted to "\$money". I tried using StringEscapeUtil.escape but it doesn't work with characters such as $.

도움이 되었습니까?

해결책

다른 팁

Prepend the \\Q in front of the string, and \\E at the end:

"\\Q$money\\E"

This tells the regex engine that the string between \Q and \E must be interpreted verbatim, ignoring any metacharacters that it may contain.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top