如何从脚本中提取IP地址。

huangapple go评论71阅读模式
英文:

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()
    }

huangapple
  • 本文由 发表于 2023年3月7日 22:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定