Kotlin Regex to Extract after and before a specific character - string

In Kotlin, i need to find the string value after and before of specific character,
For example,
Item1. myResultValue: someother string
So in the above, I need to get the value after ". " and before ":" SO my output should be "myResultValue". If it doesn't match it can return empty or null value.
Please help to write to Regex for this.

For your specific case you can use regex:
val text = "Item1. myResultValue: someother string"
val pattern = Pattern.compile(".*\\.(.*):.*")
val matcher = pattern.matcher(text)
if (matcher.matches()) {
println(matcher.group(1))
}
If you want to build something smarter that defines the start and end pattern you can do it like this:
val startSearch = "\\."
val endSearch = ":"
val text = "Item1. myResultValue: someother string"
val pattern = Pattern.compile(".*$startSearch(.*)$endSearch.*")
val matcher = pattern.matcher(text)
if (matcher.matches()) {
println(matcher.group(1))
}

Related

Node split string by last space

var str="72 tocirah abba tesh sneab";
I currently have this string and want a new string that is called "72 tocirah abba tesh". What is the best way to do this in node/Javascript?
Another one-liner:
"72 tocirah abba tesh sneab".split(" ").slice(0, -1).join(" ")
Basically, you split the string using the space separator, slice the resulting array removing the last item, and join the array using the space separator.
You can use replace, like:
"72 tocirah abba tesh sneab".replace(/\s\w+$/, '')
(This replaces the last space and word with an empty string)
I would solve it like that:
let str = "72 tocirah abba tesh sneab"
Split by " ":
let list = str.split(" ") // [72,tocirah,abba,tesh,sneab]
Remove the last element using pop():
list.pop() // [72,tocirah,abba,tesh]
Then join it back together:
str = list.join(" ") // 72 tocirah abba tesh
Following is one of the ways to handle this.
use lastIndexOf function
const lastSpacePosition = str.lastIndexOf(" ");
// in case there is no space in the statement. take the whole string as result.
if(lastSpacePosition < 0) {
lastSpacePosition = str.length;
}
str.substr(0, lastSpacePosition);
There are other ways to handle this using regEx, split->join as well

How to replace a string with star except first character in kotlin

I want to know how can I replace a given string with stars except first character of string in kotlin
For e.g i have string "Rizwan" , I want it to be R*****
You can do it with padEnd():
val name = "Rizwan"
val newName = name[0].toString().padEnd(name.length, '*')
Result:
"R*****"
Try replace center of String like Phone number. with ★:
phone.replaceRange(2 , phone.length-3 , "★".repeat(phone.length-5))
pay attention to 2 + 3 = 5 :D
Result:
"09★★★★★229"
Try replacing (?<=.). with *:
val input = "Rizwan"
val output = input.replace(Regex("(?<=.)."), "*")
println(output)
This prints:
R*****
The lookbehind (?<=.) in the regex pattern ensures that we only replace a character if at least one character precedes it. This spares the first character from being replaced.
I am not an expert in Kotlin so this may not be the best way to do it but it will work for sure.
var s = "Rizwan"
var l = s.length
val first = s[0]
s=""
while(l>1) {
s=s+"*"
l--
}
s=first+s
print(s)
Basic Algorithm..... using no library or functions
val name = "Rizwan"
val newName = name[0].toString().padEnd(name.length, '*')
Result:
"R*****"

Splitting String with multiple spaces

I want to separate a string containing two words separated by one or more spaces.
But unfortunately it doesn't work as expected, at the end only one string will result.
I read a file that always has two words in a line. It looks like this: "word1 word2".
getData() returns a List[Int, String] whereby the string contains the two words.
As already mentioned, these two words can be separated by one or more spaces.
val myMap = getData("MyFile.txt").map{ line => val tempList = line._2.split(" +")
println(line)
println(tempList(0))
(tempList(0), tempList(1).toInt)
}.toMap
Result of the prints:
(13,word1 word2)
word1 word2
is this what you need? it seems nothing wrong!
as show in the picture
val a = "word1 world2"
val b = a.split(" +")
println(b(1))
Is this the answer you need?
import scala.io.Source
object Test{
def main(args: Array[String]): Unit = {
val filename = "C:\\src/com/practice/MyFile.txt"
val lines = Source.fromFile(filename).getLines.mkString
val contents = lines.split(" +");
print(contents(1))
}
}

Removing characters from List of Strings

To remove characters from a List of Strings I use :
val validLines : List[String] = List("test[" , "test]")
val charsToClean: List[String] = List("\"", "[", "]", "'")
val filtered = validLines.map(line => line.replace(charsToClean(0), "")
.replace(charsToClean(1), "")
.replace(charsToClean(2), "")
.replace(charsToClean(3), ""))
I'm attempting use a inner map function instead of hard coding the positions of the chars to replace :
val filtered1 : List[String] = validLines.map(line => charsToClean.map {c => line.replace(c , "") })
But receive compiler error :
mismatch; found : List[List[String]] required: List[String]
Should result of line.replace(c , "") not be returned ?
No, because your code is more like: for every string and for every unwanted char, return replacement for this very symbol (n^2 strings).
What you probably wanted to do can be achieved using the following snippet:
val rawLines : List[String] = List("test[" , "test]")
val blacklist: Set[Char] = Set('\"', '[', ']' ,''')
rawLines.map(line => line.filterNot(c => blacklist.contains(c)))
// res2: List[String] = List(test, test)
Or alternatively, using regexp, as #ka4eli has shown in his answer.
replaceAll can contain more complex regex constructed with | (or) operator:
validLines.map(_.replaceAll("\\[|\\]|'|\"", ""))
You need to use foldLeft instead of map.
val validLines : List[String] = List("test[" , "test]")
val charsToClean: List[String] = List("\"", "[", "]", "'")
val filtered : List[String] = validLines.map(line => charsToClean.foldLeft(line)(_.replace(_, "")))
Equivalent to #om-nom-nom though using a for comprehension syntax; given a set of characters to erase,
val charsToClean = """"[]'""".toSet
then
for (l <- validLines) yield l.filterNot(charsToClean)
where implicit set inclusion Char => Boolean function is applied in filterNot.

Scala/Spark efficient partial string match

I am writing a small program in Spark using Scala, and came across a problem. I have a List/RDD of single word strings and a List/RDD of sentences which might or might not contain words from the list of single words. i.e.
val singles = Array("this", "is")
val sentence = Array("this Date", "is there something", "where are something", "this is a string")
and I want to select the sentences that contains one or more of the words from singles such that the result should be something like:
output[(this, Array(this Date, this is a String)),(is, Array(is there something, this is a string))]
I thought about two approaches, one by splitting the sentence and filtering using .contains. The other is to split and format sentence into a RDD and use the .join for RDD intersection. I am looking at around 50 single words and 5 million sentences, which method would be faster? Are there any other solutions? Could you also help me with the coding, I seem to get no results with my code (although it compiles and run without error)
You can create a set of required keys, look up the keys in sentences and group by keys.
val singles = Array("this", "is")
val sentences = Array("this Date",
"is there something",
"where are something",
"this is a string")
val rdd = sc.parallelize(sentences) // create RDD
val keys = singles.toSet // words required as keys.
val result = rdd.flatMap{ sen =>
val words = sen.split(" ").toSet;
val common = keys & words; // intersect
common.map(x => (x, sen)) // map as key -> sen
}
.groupByKey.mapValues(_.toArray) // group values for a key
.collect // get rdd contents as array
// result:
// Array((this, Array(this Date, this is a string)),
// (is, Array(is there something, this is a string)))
I've just tried to solve your problem and I've ended up with this code:
def check(s:String, l: Array[String]): Boolean = {
var temp:Int = 0
for (element <- l) {
if (element.equals(s)) {temp = temp +1}
}
var result = false
if (temp > 0) {result = true}
result
}
val singles = sc.parallelize(Array("this", "is"))
val sentence = sc.parallelize(Array("this Date", "is there something", "where are something", "this is a string"))
val result = singles.cartesian(sentence)
.filter(x => check(x._1,x._2.split(" ")) == true )
.groupByKey()
.map(x => (x._1,x._2.mkString(", ") )) // pay attention here(*)
result.foreach(println)
The last map line (*) is there just beacause without it I get something with CompactBuffer, like this:
(is,CompactBuffer(is there something, this is a string))
(this,CompactBuffer(this Date, this is a string))
With that map line (with a mkString command) I get a more readable output like this:
(is,is there something, this is a string)
(this,this Date, this is a string)
Hope it could help in some way.
FF

Resources