简体   繁体   English

如何使用Scala正则表达式模式匹配

[英]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. 我在scala中有一个低于String的字符串,我想搜索并用双引号“ BELL”替换值BELL。Iam使用replace但不能正常工作,使用正则表达式从头到尾检查确切搜索字符串的语法是什么? 。

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. 我的第一个搜索是BELL->“ BELL”,下一个搜索是BELL。*->“ BELL。 ”,我的第一个搜索替换应该只更新BELL,而第二个搜索和替换应该更新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\"")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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