fork(1) download
  1. import java.util.*
  2.  
  3. fun extractCurrencyAndAmount(input: String): Triple<String, String, Boolean> {
  4. val regex = Regex("""^(\D*)(\d+)(\D*)$""")
  5. val matchResult = regex.find(input)
  6.  
  7. return if (matchResult != null) {
  8. val (before, amount, after) = matchResult.destructured
  9. val currency = if (after.isEmpty()) before.strip() else after.strip()
  10. val isCurrencyAtFront = after.strip().isEmpty()
  11. Triple(currency, amount, isCurrencyAtFront)
  12. } else {
  13. throw IllegalArgumentException("Invalid input format")
  14. }
  15. }
  16.  
  17. fun main(args: Array<String>) {
  18. println( extractCurrencyAndAmount("\$100") )
  19. println( extractCurrencyAndAmount("100 \$") )
  20.  
  21. }
Success #stdin #stdout 0.13s 39388KB
stdin
Standard input is empty
stdout
($, 100, true)
($, 100, false)