Kotlin-Regex And Java Interoperability

Introduction

 
In this part, we are going to learn about Kotlin Regex and Java Interoperability. Those who are new to Kotlin and willing to learn, go through this article series starting here: Introduction to Kotlin.
 

Kotlin Regex

 
Regex is used to refer to a regular expression that is used to search a string or replace on regex object. To use it, we need to use Regex(pattern: String). To use that, we need  Kotlin.regex.text package too.
 

Kotlin Regex Constructor

  • Regex(pattern: String) - It will give string pattern.
  • Regex(pattern: String, option: RegexOption) - It will give string pattern and single option.
  • Regex(pattern: String, options: Set<RegexOption>) - It will give string pattern and set of give options.
Example
  1. fun main(args: Array < String > ) {  
  2.     val regex = ""  
  3.     "x([yz]+)a?"  
  4.     "".toRegex()  
  5.     val matched1 = regex.matches(input = "bcdp")  
  6.     val matched2 = regex.matches(input = "bcdbcd")  
  7.     val matched3 = regex.matches(input = "xyza")  
  8.     println(matched1)  
  9.     println(matched2)  
  10.     println(matched3)  
  11. }  
Output
 
false
false
true
 

Kotlin Regex Pattern

 
Regex patterns are given below.

  • x|y - x or y
  • xy - x followed by y
  • [xyz]- x,y,z
  • [x-z]- x to z
  • [^x-z]- outside the range of x-z
  • ^xyz- xyz at beginning of the line
  • xyz$- xyz at end of line
  • .- any single character

Java Interoperability 

 
Kotlin code is interoperability with Java code. The Java code can be called from Kotlin code and Kotlin code is also called from Java code simultaneously.
 
 

Calling Java code from Kotlin 

While calling the Java code from Kotlin file, it returns the result in the same types.
 
Example 
 
Myabuinteroperability.kt
  1. package myabuinteroperability package  
  2. import myjavapackage.MyJavaClass  
  3. fun main(args: Array < String > ) {  
  4.     val area: Int = MyJavaClass.area(54)  
  5.     println("printing area from java inside Kotlin file: " + area)  
  6. }  
MyJavaClass.java
  1. package myjavapackage;  
  2. public class MyJavaClass {  
  3.     public static void main(String[] args) {}  
  4.     public static int area(int l, int b) {  
  5.         int result = l * b;  
  6.         return result;  
  7.     }  
  8. }  
Output
 
printing area from java inside Kotlin file: 20
 

Calling Kotlin code from Java

 
MyKotlin.kt
  1. fun main(args: Array < String > ) {}  
  2. fun area(l: Int, b: Int): Int {  
  3.     return l * b  
  4. }  
MyJava.java
  1. public class MyJava {  
  2.     public static void main(String[] args) {  
  3.         int area = MyKotlinKt.area(65);  
  4.         System.out.print("printing area inside Java from Kotlin : " + area);  
  5.     }  
  6. }  
Output
 
printing area inside Java from Kotlin: 30 
 

Conclusion

 
Kotlin is quickly becoming a complete modern programming language. In this article, we learned about Regex and Java interoperability in Kotlin. In the next part of this series, we will learn about OOP Concept of Kotlin. Stay tuned!


Similar Articles