Question about scala's regex

Hello

I am pretty family with PCRE (perl’s regex expression), all my data cleaning was done with this way.

I want to ask if Scala’s regex policy is the same as PCRE?

Thank you.

And, for this regex definition:

val re1 = """Book: title=([^,]+),\s+author=(.+)""".r

Why the string must be enclosed with “”", not a single "?

Thank you

Scala regexes are Java regexes. There are some differences with Perl, but you don’t need a Scala-specific resource on this, there are plenty of “Perl vs Java” regex sources out there on the net.

As for the triple quotes, they’re not strictly necessary, they’re just handy for avoiding having to double your backslashes:

scala 2.13.8> "Book: title=([^,]+),\s+author=(.+)"
                                    ^
              error: invalid escape character

scala 2.13.8> """Book: title=([^,]+),\s+author=(.+)"""
val res3: String = Book: title=([^,]+),\s+author=(.+)

scala 2.13.8> "Book: title=([^,]+),\\s+author=(.+)"
val res4: String = Book: title=([^,]+),\s+author=(.+)

Python has this same feature… perhaps other languages as well.

3 Likes