简体   繁体   中英

Regex is not matching in Scala

I want to split up a camelCase string with spaces.

"ClassicalMusicArtist" -> "Classical Music Artist"

I should be able to do this by replacing "/([az](?=[AZ]))/g" with "$1 " ( regex101 ).

But my regex is not getting any matches:

val regex = "/([a-z](?=[A-Z]))/g".r
val s = "ClassicalMusicArtist"

regex.replaceAllIn(s, "$1 ") // -> Returns "ClassicalMusicArtist"
regex.findFirstIn(s) // -> Returns None

What am I doing wrong? I used the regex in another language with success and can't figure out why I am not getting any matches.

Ok I figured it out.

In scala the regex has to be val regex = "([az](?=[AZ]))".r without the leading / and the modifier.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM