我在使用正则表达式方面遇到麻烦。我的示例数据是:
12 13 hello hiiii hhhhh
this doesnt have numeric so should be removed
Even this line should be excluded
`12` this line contains numeric shouldn't exclude
Hope even this line should be excluded
scala> val pattern = "[a-z][A-Z]".r
模式:scala.util.matching.regex = [a-z] [a-z]
scala> val b = a.filter(line => !line.startsWith(pattern))
<console>:31: error: type mismatch;
找到:scala.util.matching.regex 必需:字符串 val b = a.filter(line =>!line.startswith(模式(( ^
,或者如果我使用
scala> val b = a.filter(line => !line.startsWith("[a-z][A-Z]".r)).take(3)
:29:错误:键入不匹配;
找到:scala.util.matching.regex
必要:字符串
val b = a.filter(line => !line.startsWith("[a-z][A-Z]".r)).take(3) ^
我实际上不确定如何确切地在Spark中使用Regex。请帮助我。
您的正则表达式只能与小写的单词匹配,然后由大写组成。即AA,BA,RF等
因此,您可能需要将其更改为:
[a-zA-Z]*
因此,它将匹配仅由字母(较低和大写(组成的任何单词
然后,关于比赛问题,您使用的是错误的方法,匹配正则匹配的方法是这样的:
val pattern = """[a-zA-Z]*""".r
val filtered = rdd.filter(line => !pattern.pattern.matcher(line).matches)
和这里的输出:
scala> filtered.foreach(println)
12
13
您可以在此处检查API