Beautify your regex
Say No to Pattern.compile
and instead of
Pattern.compile("^((?!(\\{.*\\})).)*$")
use scalish way:
"^((?!(\\{.*\\})).)*$".r
Say No to \\
in regex and instead of
"^((?!(\\{.*\\})).)*$"
use """
way:
"""^((?!(\{.*\})).)*$"""
Return values without val
/var
You can simply write your containsTag
without val
and result of last line will return from function - it will return your's matches:
def containsTag(text: String): Boolean = {
! """^((?!(\{.*\})).)*$""".r.matches(text)
}
Choose your destiny
Sometimes it is good to use if
/else
but more readable match
/case
:
containsTag(text) match {
case true =>
val randomPosition = Random.nextInt(allCharacters.length - 1)
text.replace("}", allCharacters(randomPosition).toString)
case _ =>
text
}
Feel the power
Collect it all together:
val allCharacters = "qwertyuiopasdfghjklzxcvbnmQWERTY" +
"UIOPASDFGHJKLZXCVBNM1234567890!@#%*()_+-=[]{x}|?.;:`"
def removeTags(text: String): String = {
def containsTag(text: String): Boolean = {
! """^((?!(\{.*\})).)*$""".r.matches(text)
}
containsTag(text) match {
case true =>
val randomPosition = Random.nextInt(allCharacters.length - 1)
text.replace("}", allCharacters(randomPosition).toString)
case _ =>
text
}
}
val res = removeTags("my friendly {name} is $userNickname$")
For random chars
According to answer in Replacing characters in a String in Scala you can use construction without while loop but with map
+ lambda
like in code below:
val allCharacters = "+=-?"
def removeTags(text: String): String = {
def containsTag(text: String): Boolean = {
! """^((?!(\{.*\})).)*$""".r.matches(text)
true
}
containsTag(text) match {
case true =>
text.map(
(c) => c match {
case '{' =>
val randomPosition = Random.nextInt(allCharacters.length - 1)
allCharacters(randomPosition)
case c => c // for another char which is not == {
}
)
case _ =>
text
}
}
Then it will be good to divide functionality and create new Char => Char
transformer function from anonymous (c) => c match {..}
which we lay in text.map(...)
:
//...
def replaceRandom(c: Char): Char = {
c match {
case '{' =>
val randomPosition = Random.nextInt(allCharacters.length - 1)
allCharacters(randomPosition)
case c => c
}
}
containsTag(text) match {
case true => text.map((c) => {replaceRandom(c)})
case _ => text
}
//...
Simplify code in two parts.
Part 1: simplify match/case
in replaceRandom(c: Char): Char
:
def replaceRandom(c: Char): Char = {
c match {
case '{' =>
val randomPosition = Random.nextInt(allCharacters.length - 1)
allCharacters(randomPosition)
case c => c
}
}
// remove unneeded "{" and "}"
def replaceRandom2(c: Char): Char = c match {
case '{' =>
val randomPosition = Random.nextInt(allCharacters.length - 1)
allCharacters(randomPosition)
case c => c
}
Then part 2: simplify map((c) => {replaceRandom(c)})
:
containsTag(text) match {
case true => text.map((c) => {replaceRandom(c)})
case _ => text
}
//remove unneeded "{" and "}"
containsTag(text) match {
case true => text.map((c) => replaceRandom(c))
case _ => text
}
//remove unneeded "(c)" to wildcard "_"
containsTag(text) match {
case true => text.map(replaceRandom(_))
case _ => text
}
//remove unneeded wildcard "_"
containsTag(text) match {
case true => text.map(replaceRandom)
case _ => text
}
Feel the power again
So for your task i will chose that variant: all functions are divided, logic placed is simple if/else and now good readable (as you see now it good readable also with if/else as i wrote to you above). After, when logic will grow you can use instead if/else match/case.
def removeTags(text: String): String = {
def containsTag(text: String): Boolean = {
! """^((?!(\{.*\})).)*$""".r.matches(text)
true
}
def replaceRandom(c: Char): Char = c match {
case '{' =>
val randomPosition = Random.nextInt(allCharacters.length - 1)
allCharacters(randomPosition)
case c => c
}
if (containsTag(text))
text.map(replaceRandom)
else text
}
val allCharacters = "+=-?"
val e = "my fr{{{iend{{{ly {{name} is $userNickname$"
println(removeTags(e))
P.S. or place to think
- Maybe your regex not working? Check it at: Regex101
- Why you need to replace
}
symbol with another random from allCharacters
which can be also }
?
- The bad, the good and the
!
: maybe try to rewrite your !noTagsRegex
to noTagsRegex
? It can be more readable for another people.
Also i want to see additions to my review too, that code is too verbose, so welcome!
sometext {name} sometext
(look at: regex101.com/r/2PR2kC/1) and in next you replace"}"
for random symbol fromallCharacters
where you also has curly braces. \$\endgroup\$