https://kotlinlang.org logo
Title
s

Shabinder Singh

04/12/2023, 11:05 PM
A working pattern from java, is not working when used in commonSrc in kotlin.text.Regex Pattern String:
nma=function\\(\\w\\)(\\{.*?return b.join\\(\\\"\\\"\\)\\}\\;)
Compiled Pattern:
nma=function\(\w\)(\{.*?return b.join\(\"\"\)\}\;)
Kotlin: v1.8.0 matching over text in js.txt. all online regex matchers find this valid. but kotlin is returning null, even documentation says it should compile to jvm pattern when run on jvm. Not very much experience with regexs, any help is appreciated.
a

Anonymike

04/13/2023, 12:00 AM
Hi there...I am very familiar with Regex and the following in scratch kotlin file does return a match correctly:
import java.io.File

val text = File("/home/perpetual/Downloads/js.txt").readText()

val regex = "nma=function\\(\\w\\)(\\{.*?return b.join\\(\\\"\\\"\\)\\}\\;)".toRegex()

// println(text)

regex.findAll(text).forEach {
    println(it.value)
}
If you'd like to learn more about how this regex works, this is a very useful tool that gives an explanation of the entire regex: https://regex101.com/r/X2Gkky/1
s

Shabinder Singh

04/13/2023, 1:09 AM
Its works if used in jvm directly, but not running in commonSrc in multiplatform for some reason.
a

Anonymike

04/13/2023, 4:37 AM
I see...I thought it was working in Java code, but not Kotlin by the wording. Is there a particular target you are unable to run it in?
Different targets no doubt use different regex engines, but they should be configured to work similarly