I want to use regular expressions in Kotlin and do the following:
"0012" == "12" // I want to make the above true
When comparing here, we are trying to compare after removing 00 of prefix in "0012" with regular expression.
As a result, it became as follows.
val reg = Regex ("0 * ([0-9] +)") reg.matchEntire ("00123") ?. groups? .get (1) ?. value == "123"
Is there a way to write this more concisely?
I would be grateful if you could tell me any good way.
Answer # 1
I don't know anything about the language, but as long as I google it,
"00123" .replace ("^ 0 *". toRegex (), "")
I think you can replace it like this.
Answer # 2
Do you compare with Int?
print ("123" .toInt () == "00123" .toInt ()) // =>true
Answer # 3
It is premised that no symbols are included, but it is embedded in regular expressions.
val a = "0012" val b = "12" a.matches (Regex ("0 * $b"))
- Interpretation of common regular expression knowledge points and interpretation of valid numbers and mobile phone number mailbox
- JAVA regular expression matching, replacement, search, cutting method
- Detailed explanation of linux grep regular expression and grep usage
- Write a summary of efficient regular expression skills
- Regular expression operator precedence introduction
- JS uses regular expression to implement keyword replacement bold function example
- Python regular expression learning guide
- Constructing regular expression validation using exec () method in JS
- Regular expression link replacement functions
- Detailed regular expression (RegExp)
- Detailed PHP regular expression replacement implementation (PHP preg_replace, PHP preg_replace)
- js regular expression basic syntax (exquisite)
- ASP regular expression method to clear HTML specified tags
- The basics of a complete PHP regular expression tutorial
- PHP regular expression tutorial (recommended)
- Use regular expression NSRegularExpression in iOS to validate textfiled input
- Android regular expression
- Php get regular expression for hyperlink text content (five methods)
- Introduction to regular expression assertions, inspections, forward assertions, and reverse assertions
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- android studio - unresolved reference comes out in kotlin
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- python - importerror: cannot import name md5 error cannot be resolved