Can somebody spot what's the problem I'm always ge...
# getting-started
a
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
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
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
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
๐Ÿ˜ฎ
v
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
Thanks!
v
Actually if you target JVM 8+ ๐Ÿ™‚
JS does not support it
n
also...why are you regex'ing a class file?
a
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
ah