简体   繁体   中英

How to use scala regex pattern matching

I have a below String in scala, I want to search and replace value BELL with double quotes "BELL".Iam using replace but it doesn't work correctly, whats the syntax to use regex that checks the exact search string from start to end.

my first search is with BELL -> "BELL" and next search will be for BELL.* -> "BELL. ", my first search replace should update just BELL and second search and replace should update BELL.

val str = "(((( EMP = BELL) OR ( LASTNAME = BELL) OR ( LASTNAME = BELL)) OR ( ( EMPFIRSTNAME = BELL.*)))"

str.replace("BELL","""""""+"BELL"+""""""")

( ( ( ( EMP = "BELL") OR ( LASTNAME = "BELL") OR ( LASTNAME = "BELL")) OR ( ( EMPFIRSTNAME = **"BELL."***)))

Try this, use the backslash to escape double quotes.

val str = "(((( EMP = BELL) OR ( LASTNAME = BELL) OR ( LASTNAME = BELL)) OR ( ( EMPFIRSTNAME = BELL.*)))"
val regex = "BELL((\\.)(\\*))*"
val replaced = str.replaceAll(regex, "\"BELL$2\"")

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