Regex for hierarchal string in Kotlin - android

I want to create Regex for string in Kotlin:
%3D and %26 are delimiters unicode for = &.
Input String: urn:abc:12345?id%3Dabc%26name%3Dwonder%26
Regex: urn:abc:([a-z]{1,31}):(\d+)(?:?)(?:([\w. _ -]+)(?:=|%3D)([\w. _ - :[] - ]+)(?:&|%26)+)

Related

How to change styles for all numbers in string in Kotlin

How to change styles for all numbers in strings (from strings.xml) to (small) and (subscript) and (color.blue)? and where put that in recyclerView adapter (all strings in Array)?
Like this:
If you are using Kotlin, use regex to replace all the numbers with HTML formatting.
var text = "This is an example of text[1] formatting."
"\\[[0-9]+\\]".toRegex().findAll(text)
.flatMap { it.groupValues }
.forEach {
val num = it.drop(1).dropLast(1)
text = text.replace(it, "<sup>[$num]</sup>")
}
Then use tvMyTextView.setText(Html.fromHtml(text))

Is it possible use text with annotation?

Is it possible use text (not string resource) with annotation?
For example: val text = "Open the page <annotation clickable="page">123</annotation>"
I want get spans from this text like in this article:
val annotations = spannedString.getSpans(
0,
spannedString.length,
android.text.Annotation::class.java
)
Is it possible use text (not string resource) with annotation? For example: val text = "Open the page <annotation clickable="page">123</annotation>"
Yes it's possible by setting the annotations to portions of a SpannableString using setSpan & marking the span with the Annotation class:
val spannedString = SpannableString("Open the page 123")
spannedString.setSpan(Annotation("key", "clickable"), 14, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
val annotations = spannedString.getSpans(
0,
spannedString.length,
android.text.Annotation::class.java
)

Regex matches always returns false

I'm trying to validate user input so that the only allowed characters in a string are A-Z, a-z, _, - and whitespace. To do that I wrote the following code:
val regex = Regex("[\\w\\s-]")
val flag = regex.matches("Hello Overlay")
But the value of flag is false and I can't figure out why.
To match the whole string meeting a pattern use
val regex = Regex("[\\w\\s-]+")
Or, to avoid overescaping:
val regex = Regex("""[\w\s-]+""")
See the Kotlin demo. Note that matches requires a full string match, but [\w\s-] only matches a single character.
val regex = Regex("""[\w\s-]+""")
val flag = regex.matches("Hello Overlay")
println(flag) // => true
val regex = Regex("""[\w\s-]+""")
val flag = regex.matches("Hello Overlay")
println(flag) // => true

How to convert char to ascii value in kotlin language

I am developing an android application with kotlin in which I need to convert an string character to its ASCII value,
fun tryDiCript(cypher: String) :String {
var cypher = "fs2543i435u#$#g###sagb#!#12416###"
var originalText = ""
var regEx =Regex("[a-z]")
for(char in regEx.findAll(cypher))
{
originalText += (char.value.toInt()).toString()
}
return originalText
}
this tutorial website showed me to use char.toInt() but it gives runtime error saying
Caused by: java.lang.NumberFormatException: Invalid int: "u"
so how if anyone knows hot to convert char to ASCII value please help me.
char.value is a String. When you call String.toInt(), it is expecting a numeric string such as "1", "-123" to be parsed to Int. So, "f".toInt() will give you NumberFormatException since "f" isn't a numeric string.
If you are sure about char.value is a String containing exactly one character only. To get the ascii value of it, you can use:
char.value.first().code
Since Kotlin 1.5
if your variable is of type char for example 'a' you can simply use a.code.
The old methods (e.g. toByte()) are deprecated now.
You said ascii, not unicode. So it's easy.
This is an example that shows you how to convert a char ('A') to it's ascii value.
fun main(vararg args: String) {
println('A'.toByte().toInt())
}
The output is what we expected, 65.
Note this doesn't work with unicode.
Edit 1
I guess this to work.
fun tryDiCript(cypher: String): String {
var cypher = "fs2543i435u#$#g###sagb#!#12416###"
var originalText = ""
var regEx = Regex("[a-z]")
for(char in regEx.findAll(cypher))
originalText += char.value[0].toInt().toString()
return originalText
}
And I recommend you to use StringBuilder.
fun tryDiCript(cypher: String): String {
var cypher = "fs2543i435u#$#g###sagb#!#12416###"
val originalText = StringBuilder()
var regEx = Regex("[a-z]")
for(char in regEx.findAll(cypher))
originalText.append(char.value[0].toInt())
return originalText.toString()
}
I checked #ice1000's answer, I found the block below does not work.
fun main(vararg args: String) {
println('A'.toByte().toInt())
}
As we can see in the Kotlin Documentation String - Kotlin Programming Language,the toByte() function of String "Parses the string as a signed Byte number and returns the result." If the the content of the string is not a number, it will throw a java.lang.NumberFormatException.
But there is another function of String called toByteArray(),this function does no require the content of the string being numbers. My code is as following:
String tempString = "Hello"
val tempArray = tempString.toByteArray()
for (i in tempArray){
println(i.toInt())
}
Attention that toByteArray() function's definition in kotlin's documentaion:
fun String.toByteArray(
charset: Charset = Charsets.UTF_8
): ByteArray
The default charset is UTF-8, if you would like to use other charset, you can modify it with the parameter.

How to apply RegEx to whole string?

Is regular expression matching every single string?
For example:
String s = "android4you2 and 4us";
What's the RegEx that I have to write to make "s" only for [a-z]?
Your question is unclear, to match the whole string, you could do:
if (s.matches("[a-zA-Z0-9 ]+")) { ...
If you want to only test for alphabetic characters:
if (s.matches("[a-zA-Z]+")) { ...
try using this.
String regex = "^[A-Za-z\\s]+$";

Categories

Resources