https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Animesh Sahu

12/22/2020, 10:07 AM
Can somebody spot what's the problem I'm always getting null using this regex:
Copy code
val output = (...)
 
 """public \w*\s*class (.+)\.(\w+) \{\n([^\}]*)\}$""".toRegex().find(output)
Here's the thing to match against:
Copy code
Compiled from "Test.kt"
public final class com.github.animeshz.keyboard.jni.Test {
  public final native void test(kotlinx.coroutines.flow.Flow<java.lang.Integer>);
  public com.github.animeshz.keyboard.jni.Test();
}
Seems that regex is correct according to this https://regex101.com/r/PEv4BL/2
🐛 1
👍 1
v

Vampire

12/22/2020, 10:22 AM
Your regex101 test is not too meaningful as you use the PCRE dialect which is different. But for your regex indeed it is the same and works fine. I tested this in REPL and as you can see it returns a match result:
Copy code
val output = """
Compiled from "Test.kt"
public final class com.github.animeshz.keyboard.jni.Test {
  public final native void test(kotlinx.coroutines.flow.Flow<java.lang.Integer>);
  public com.github.animeshz.keyboard.jni.Test();
}
""".trimIndent()
"""public \w*\s*class (.+)\.(\w+) \{\n([^\}]*)\}$""".toRegex().find(output)
res0: kotlin.text.MatchResult? = kotlin.text.MatcherMatchResult@6c37c36
Do you maybe have a trailing newline character that then does not match due to
\}$
not allowing anything after the curly brace?
a

Animesh Sahu

12/22/2020, 10:25 AM
image.png
Adding optional new line character, or removing the $ yeilds the same thing
By optional
*
sorry I wrote + 😅
Oh wait, maybe that's windows \r\n
Yes!! I'm tired of this, don't know why microsoft wants to put \r for no reason -_-
v

Vampire

12/22/2020, 10:30 AM
Well, that's the natural translation from pre-computer era
😅 1
👀 1
On a typewriter you made a carriage return to be on the left and a new line to be on the next line
This translates to
\r\n
Apple thought they are clever and could leave out the
\n
, only using
\r
as line terminator (in the meantime switched to
\n
)
Linux thought the same and left out
\r
only using
\n
a

Animesh Sahu

12/22/2020, 10:33 AM
😮
v

Vampire

12/22/2020, 10:35 AM
If your target is only JVM, you could actually use
\R
in the regex:
Any Unicode linebreak sequence, is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]
👍 1
a

Animesh Sahu

12/22/2020, 10:36 AM
Thanks!
v

Vampire

12/22/2020, 10:41 AM
Actually if you target JVM 8+ 🙂
JS does not support it
n

nanodeath

12/22/2020, 5:15 PM
also...why are you regex'ing a class file?
a

Animesh Sahu

12/23/2020, 2:52 AM
Not really a class file but a javap output, for automated generation of JNI headers.
And that's cuz there's no tool to generate headers for Kotlin right now https://stackoverflow.com/questions/48816188/kotlin-replacement-for-javah answers are outdated, and that lib is probably not maintained.
n

nanodeath

12/23/2020, 3:35 AM
ah
29 Views