0

I want to write a custom regex for scalastyle so that it could catch cases when a developer forget a whitespaces before and after operators, but the regex shouldn't catch anything inside the string. Let's see the examples:

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

Here is what I have so far, but it doesn't handle strings properly:

([a-zA-Z0-9]+([-+&<>!=\/&%]+)[a-zA-Z0-9]|[a-zA-Z0-9]([-+&<>!=\/&%]+)|([-+&<>!=\/&%]+)[a-zA-Z0-9])
alex
  • 942
  • 1
  • 10
  • 26
  • 1
    This is very hard to do with just regex, although possible with PCRE. The easiest pattern might look like `/(?<!\\)(?:\\{2})*"[^"\\]*(?:\\.[^\\"]*)*"|(?<=[^-+&<>!=\/&%\s])([-+&<>!=\/&%]+)|([-+&<>!=\/&%]+)(?=[^-+&<>!=\/&%\s])/`, see [demo](https://regex101.com/r/rW7Ozv/4), but inside the code, you need to see if Group 1 or 2 matched, and if yes, the match is valid, else, the match is invalid (this is necessary to reject matches inside string literals). – Wiktor Stribiżew Feb 08 '19 at 13:07
  • 1
    A PCRE pattern that can cope with the problem is `(?<!\\)(?:\\{2})*"[^"\\]*(?:\\.[^\\"]*)*"(*SKIP)(*F)|(?<=[^-+&<>!=\/&%\s])[-+&<>!=\/&%]+|[-+&<>!=\/&%]+(?=[^-+&<>!=\/&%\s])`, see [demo](https://regex101.com/r/rW7Ozv/5). – Wiktor Stribiżew Feb 08 '19 at 13:07

0 Answers0