在scala中使用正则表达式提取字符串

时间:2022-06-01 12:46:01

I have a use case where I would like to extract certain string from an input in scala.

我有一个用例,我想从scala中的输入中提取某些字符串。

My input string looks something like:

我的输入字符串看起来像:

asdwf:"ssdf", as232:"ss",ABC:"xxx",sdfsf234:"sdaf"

I would like to extract the xxx after ABC.

我想在ABC之后提取xxx。

I tried defining a regex match pattern:
val Pattern = """ABC:"(.*)",""".r, but got ABC:"xxx",sdfsf234:"sdaf" as output string.

我尝试定义正则表达式匹配模式:val Pattern =“”“ABC:”(。*)“,”“”。r,但得到ABC:“xxx”,sdfsf234:“sdaf”作为输出字符串。

Anything I am not doing correctly?

我做得不对劲?

Thanks.

2 个解决方案

#1


0  

Try using a non-greedy expression, i.e.:

尝试使用非贪婪的表达,即:

val Pattern = """ABC:"(.*?)"""".r

Update:

I'm not a scala user and I cannot test the code, but in theory the following should replace everything with the value of ABC

我不是scala用户而且我无法测试代码,但理论上以下内容应该用ABC的值替换所有内容

val res = """ asdwf:"ssdf", as232:"ss",ABC:"xxx",sdfsf234:"sdaf"""".replaceAll(""".*ABC:"(.*?)".*""", "$1")

I took a crash course on scala and the regex seems to work:

我参加了scala的速成课程,正则表达式似乎有效:

https://ideone.com/LAlCc0

#2


2  

You can look for matches like this

你可以找这样的比赛

    val p = """[^ :,"]+[\s]*:"[^"]*"""".r
    p findAllIn """asdwf:"ssdf", as232:"ss",ABC:"xxx",sdfsf234:"sdaf""""

Now you can get an iterator with all the matches.

现在你可以得到一个包含所有匹配的迭代器。

You can extract their contents like this

你可以像这样提取他们的内容

    (p findAllIn """asdwf:"ssdf", as232:"ss",ABC:"xxx",sdfsf234:"sdaf"""").map(str => {
        val p(key, value) = str
        (key, value)
    }).toMap

#1


0  

Try using a non-greedy expression, i.e.:

尝试使用非贪婪的表达,即:

val Pattern = """ABC:"(.*?)"""".r

Update:

I'm not a scala user and I cannot test the code, but in theory the following should replace everything with the value of ABC

我不是scala用户而且我无法测试代码,但理论上以下内容应该用ABC的值替换所有内容

val res = """ asdwf:"ssdf", as232:"ss",ABC:"xxx",sdfsf234:"sdaf"""".replaceAll(""".*ABC:"(.*?)".*""", "$1")

I took a crash course on scala and the regex seems to work:

我参加了scala的速成课程,正则表达式似乎有效:

https://ideone.com/LAlCc0

#2


2  

You can look for matches like this

你可以找这样的比赛

    val p = """[^ :,"]+[\s]*:"[^"]*"""".r
    p findAllIn """asdwf:"ssdf", as232:"ss",ABC:"xxx",sdfsf234:"sdaf""""

Now you can get an iterator with all the matches.

现在你可以得到一个包含所有匹配的迭代器。

You can extract their contents like this

你可以像这样提取他们的内容

    (p findAllIn """asdwf:"ssdf", as232:"ss",ABC:"xxx",sdfsf234:"sdaf"""").map(str => {
        val p(key, value) = str
        (key, value)
    }).toMap