首页 > 解决方案 > 用于 scalastyle 的自定义正则表达式以捕获丢失的空格

问题描述

我想为 scalastyle 编写一个自定义正则表达式,以便它可以捕获开发人员在运算符之前和之后忘记空格的情况,但正则表达式不应该捕获字符串中的任何内容。让我们看看例子:

val asdf123=sdf // should be catched
val as = sd // shouldn't be watched
val a /=10 // should be
def check() =as // should be
def check= { // should be
val isEqual = a == b // shouldn't
val b += 2 // shouldn't
val e = true!=false //should
val a = 2!=3 //dhould
val a = 2 != 3 // shouldn't
val a = 2 <= 3 // shouldn't
val a = b&&s // should
val a = b % s // shouldn't
val a/= s // should
val a /= b // shouldn't
def validateSeq[T](input: Seq[T], validationFunc: (T =>Option[Message])*): Either[ValidationError, Seq[T]] = { // should, because of =>Option
val url = "/docs/swagger-ui/index.html?url=/docs/swagger" // shouldn't because =/ is inside string
"Access-Control-Allow-Credentials" // shouldn't

这是我到目前为止所拥有的,但它不能正确处理字符串:

([a-zA-Z0-9]+([-+&<>!=\/&%]+)[a-zA-Z0-9]|[a-zA-Z0-9]([-+&<>!=\/&%]+)|([-+&<>!=\/&%]+)[a-zA-Z0-9])

标签: regexscalastyle

解决方案


推荐阅读