我正在做一些词分析,并使用正则表达式来匹配一些不需要的词,并将其替换为“”,例如:
some common context here as single_word;
我的目标是将as single_word ;
部分替换为“”,就像这样:
some common context here
我的正则表达式为as\s[\w]*\p{L}*\w[\w.]*(\s+)?;
,我对其进行了很好的测试,但是在scala中无法正常工作,其代码为:
sentence.trim.replaceAll(s"as\\s[\\w]*\\p{L}*\\w[\\w.]*(\\s+)?;", "") .
答案 0 :(得分:1)
让我们说您有模式
val pattern ="as\\s[\\w]*\\p{L}*\\w[\\w.]*(\\s+)?;"
val str = "some common context here as single_word; "
str.replaceAll(pattern,"")
在scala工作表中,您得到的输出为
pattern: String = as\s[\w]*\p{L}*\w[\w.]*(\s+)?;
str: String = some common context here as single_word;
res0: String = some common context here
答案 1 :(得分:0)
在REPL中
scala> val pattern ="as .*;"
pattern: String = as .*;
scala> val str = "some common context here as single_word; "
str: String = "some common context here as single_word; "
scala> str.replaceAll(pattern,"")
res3: String = "some common context here "
scala>