Kotlin:
पैटर्न से मेल खाते अक्षरों को हटाना
How to (कैसे करें):
fun main() {
val originalText = "Programming in Kot1in is fun2!"
val pattern = "\\d".toRegex() // Digits ko dhundhne ke liye pattern
val sanitizedText = originalText.replace(pattern, "")
println(sanitizedText) // Output: Programming in Kotlin is fun!
}
Is code mein, \\d
ka matlab hota hai “koi bhi digit”. toRegex()
ko use karke hum is pattern ko regex mein convert karte hain. replace()
function se hum original text mein se sabhi digits ko hata dete hain.
Deep Dive (गहराई से जानिये):
Pattern matching ka istemaal bahut pehle se hota aaya hai. Unix-based systems mein grep
jaisi command-line tools iska ek udaharan hain. Kotlin mein regex ka use karke character matching aur replacing bohot aasan ban gaya hai. Alternatives mein filter
aur lambdas bhi hote hain, par regex zyada powerful hota hai khas kar jab complex patterns ki baat aati hai. Regex implementation details ke liye, Kotlin Java ki Pattern
aur Matcher
classes ka sahara leta hai.
See Also (और देखें):
- Kotlin documentation on regex: Kotlin Regex
- Java Pattern class: Java Pattern
- Tutorial on regular expressions: Regex Tutorial