英文:
how do I extract an ip address from within a script
问题
我是 Kotlin 语言的初学者。我想从文本中提取一组 IP 地址。你能帮助我吗?
var ip = string.split("[a-z]".toRegex())
println(ip)
//需要的输出
//192.168.0.1
//148.99.65.103
- var string = """
hello
192.168.0.1
test
192.168.0.22
kotlin
192.168.0.115
"""
var ip = string.split("[a-z]".toRegex())
println(ip)
//需要的输出
//192.168.0.1
//148.99.65.103
- var string = """
hello
192.168.0.1
test
192.168.0.22
kotlin
192.168.0.115
"""
英文:
I am a beginner in the Kotlin language. I want to extract a group of ip from the text. Can you help me?
var ip= sting.split("[a-z]".toRegex())
println(ip)
//need output
//192.168.0.1
//148.99.65.103
- var string ="""
hello
192.168.0.1
test
192.168.0.22
kotlin
192.168.0.115
"""
答案1
得分: 0
val regex = """((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)""".toRegex()
val str1 = "hello 192.168.0.1 test 192.168.0.22 kotlin 192.168.0.115"
var matchResult = regex.find(str1)
while (matchResult != null) {
println(matchResult!!.value)
matchResult = matchResult.next()
}
英文:
val regex = """((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)""".toRegex()
val str1= "hello 192.168.0.1 test 192.168.0.22 kotlin 192.168.0.115"
var matchResult = regex.find(str1)
while (matchResult != null) {
println( matchResult!!.value)
matchResult = matchResult.next()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论