gpt4 book ai didi

r - 遇到特殊字符时从 R 中的字符串中提取子字符串

转载 作者:行者123 更新时间:2023-12-04 11:24:54 27 4
gpt4 key购买 nike

我正在尝试为字符串中的所有表情符号提取基本名称 (U+...)。我目前有一个包含一列 Instagram 消息的数据框(出于这些原因,出于道德原因,我不能在这里发帖。但是我会发布一个自己生成的。)

我想从消息字符串中提取所有表情符号。

到目前为止,我已经成功地使用 gsub 从一段文本中提取了单个表情符号。例如:

    gsub(".*[<]([^.]+)[>].*", "\\1", "I know <U+0001F621<U+0001F923>")

这给了我最后的表情符号:
    [1] "U+0001F923"

但是我希望它能捕获字符串中的所有表情符号。

像这样:
    [1] "U+0001F923"  [2] "U+0001F621"

此外,我尝试使用此 gsub 代码从 2 列数据框中提取数据。 (下面是一个更大的数据框的片段)

df:
    name                     value
<chr> <chr>
Participant1 instahandle1
Participant2 instahandle2
conversation.sender instahandle2
conversation.created_at 2019-03-24T19:08:25.632223+00:00
conversation.text I know <U+0001F923><U+0001F923>x
conversation.sender instahandle1
conversation.created_at 2019-03-24T19:04:01.042261+00:00
conversation.text Me too! it was cool
conversation.sender instahandle2
conversation.created_at 2019-03-24T19:03:42.065983+00:00

gsub(".*[<]([^.]+)[>].*", "\\1", df$value)

然而这只是检索。
    [1] "instahandle1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
[2] "instahandle2"
[3] "instahandle2"
[4] "2019-03-24T19:08:25.632223+00:00"
[5] "I know \U0001f923\U0001f923x"
[6] "instahandle1"
[7] "2019-03-24T19:04:01.042261+00:00"
[8] "Me too! it was cool"
[9] "instahandle2"
[10] "2019-03-24T19:03:42.065983+00:00"

我希望它提取每个表情符号而不是其他任何东西。
像这样:
     [1] "U+0001F923"  [2] "U+0001F621"

最佳答案

您可以使用

x <- "I know \U0001F621\U0001F923s"
regmatches(x, gregexpr("[^[:ascii:]]+", x, perl=TRUE))
## => [[1]]
## [1] "😡🤣"

此代码从输入中提取所有非 ASCII 字符块。见 online R demo .

[:ascii:]字符类不符合 POSIX, perl=TRUE是必须的。

如果您只想单独提取表情符号,请使用
emoji_rx <- "[\\x{1f300}-\\x{1f5ff}\\x{1f900}-\\x{1f9ff}\\x{1f600}-\\x{1f64f}\\x{1f680}-\\x{1f6ff}\\x{2600}-\\x{26ff}\\x{2700}-\\x{27bf}\\x{1f1e6}-\\x{1f1ff}\\x{1f191}-\\x{1f251}\\x{1f004}\\x{1f0cf}\\x{1f170}-\\x{1f171}\\x{1f17e}-\\x{1f17f}\\x{1f18e}\\x{3030}\\x{2b50}\\x{2b55}\\x{2934}-\\x{2935}\\x{2b05}-\\x{2b07}\\x{2b1b}-\\x{2b1c}\\x{3297}\\x{3299}\\x{303d}\\x{00a9}\\x{00ae}\\x{2122}\\x{23f3}\\x{24c2}\\x{23e9}-\\x{23ef}\\x{25b6}\\x{23f8}-\\x{23fa}]"
x <- "I know \U0001F621\U0001F923s"
regmatches(x, gregexpr(emoji_rx, x, perl=TRUE))
## => [[1]]
## [1] "😡" "🤣"
## Or, to get them as single chunks
emoji_rx <- "[\\x{1f300}-\\x{1f5ff}\\x{1f900}-\\x{1f9ff}\\x{1f600}-\\x{1f64f}\\x{1f680}-\\x{1f6ff}\\x{2600}-\\x{26ff}\\x{2700}-\\x{27bf}\\x{1f1e6}-\\x{1f1ff}\\x{1f191}-\\x{1f251}\\x{1f004}\\x{1f0cf}\\x{1f170}-\\x{1f171}\\x{1f17e}-\\x{1f17f}\\x{1f18e}\\x{3030}\\x{2b50}\\x{2b55}\\x{2934}-\\x{2935}\\x{2b05}-\\x{2b07}\\x{2b1b}-\\x{2b1c}\\x{3297}\\x{3299}\\x{303d}\\x{00a9}\\x{00ae}\\x{2122}\\x{23f3}\\x{24c2}\\x{23e9}-\\x{23ef}\\x{25b6}\\x{23f8}-\\x{23fa}]+"
regmatches(x, gregexpr(emoji_rx, x, perl=TRUE))
## => [[1]]
## [1] "😡🤣"

this online R demo .

关于r - 遇到特殊字符时从 R 中的字符串中提取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57180716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com