Home > Software engineering >  Dictionary world replace in Android using HashMap
Dictionary world replace in Android using HashMap

Time:09-28

I want to create dictionary using HashMap.

My JSON which i use to convert to HashMap:

"ok": 'około ',
"g": 'gram ',
"G": 'gram ',
"gr": 'gram ',
"dkg": 'dekagram ',
"dag": 'dekagram ',
"kg": 'kilogram ',
"Szkl": 'szklanka ',
"sz": 'szklanka ',
"Mil": 'mililitr ',
"ml": 'mililitr ',
"l": 'litr ',
"szt": 'sztuk '

Currently I am using this:

val patternString = "("   join("|", wordsToReplace.keys)   ")"
val pattern: Pattern = Pattern.compile(patternString)
val matcher: Matcher = pattern.matcher(description)

val sb = StringBuffer()
while (matcher.find()) {
    val group = matcher.group(1)
    if (wordsToReplace.containsValue(group)) {
        matcher.appendReplacement(sb, wordsToReplace.getValue(group))
    }
}
matcher.appendTail(sb)
return sb.toString()

But i need to add additional regex with this conditions:

  • number before replaced word
  • space before replaced word and space after replaced word (ex. " l ")
  • dot with space (". ") or new line after replaced word

Examples:

  • "1l" - 1litr
  • " l " - litr
  • "1l." - 1litr

CodePudding user response:

As you are dynamically generating the pattern, you can either allow for all permutations and specify whitespace boundaries on the left and right:

(?<!\S)(?:\d | )?(?:ok|g|G|gr|dkg|dag|kg|Szkl|sz|Mil|ml|l|szt)[.\n ]?(?!\S)

See a regex demo.

If you want to get every specific scenario, you can create an alteration | listing all the options which can get quite long.

(?<!\S)(?:(?:ok|g|G|gr|dkg|dag|kg|Szkl|sz|Mil|ml|l|szt)|\d (?:ok|g|G|gr|dkg|dag|kg|Szkl|sz|Mil|ml|l|szt)\.?| (?:ok|g|G|gr|dkg|dag|kg|Szkl|sz|Mil|ml|l|szt) )(?!\S)

See a regex demo.

  • Related