我有帶注釋符號的話語:
utt <- c("↑hey girls↑ can I <join yo:u>", "((v: grunts))", "!damn shit! got it",
"I mean /yeah we saw each other at a party:/↓ the other day"
)
我需要拆分utt成單獨的詞,除非這些詞被某些定界符括起來,包括這個 class [(/≈↑£<>°!]。我對s使用雙重否定前瞻做得相當好,utt其中分隔符之間只出現一個這樣的字串;但是當分隔符之間有多個這樣的字串時,我無法正確拆分:
library(tidyr)
library(dplyr)
data.frame(utt2) %>%
separate_rows(utt, sep = "(?!.*[(/≈↑£<>°!].*)\\s(?!.*[)/≈↑£<>°!])")
# A tibble: 9 × 1
utt2
<chr>
1 ↑hey girls↑ can I <join yo:u>
2 ((v: grunts))
3 !damn shit!
4 got
5 it
6 I mean /yeah we saw each other at a party:/↓
7 the
8 other
9 day
該預期的結果將是:
1 ↑hey girls↑
2 can
3 I
4 <join yo:u>
5 ((v: grunts))
6 !damn shit!
7 got
8 it
9 I
10 mean
11 /yeah we saw each other at a party:/↓
12 the
13 other
14 day
uj5u.com熱心網友回復:
您可以使用
data.frame(utt2) %>% separate_rows(utt2, sep = "(?:([/≈↓£°!↑]).*?\\1|\\([^()]*\\)|<[^<>]*>)(*SKIP)(*F)|\\s ")
請參閱正則運算式演示。
請注意,在您的情況下,有配對的字符(如(and )、<and >)和非配對的字符(如↑, £)。它們需要不同的處理方式反映在模式中。
詳情:
(?:([/≈↓£°!↑]).*?\\1|\\([^()]*\\)|<[^<>]*>)(*SKIP)(*F)火柴([/≈↓£°!↑]).*?\1|- a/,≈,↑,£,°或!char 被捕獲到 Group 1 中,然后是除換行符以外的任何零個或多個字符(請參閱.*?),然后是捕獲到 Group 1 中的相同字符\([^()]*\)|-(,比其他零個或多個字符(和)再)炭,或<[^<>]*>-<,零個或多個字符比其他<和>,然后一個>字符(*SKIP)(*F)- 跳過匹配的文本并從失敗位置重新開始搜索
|- 或者\s- 任何其他背景關系中的一個或多個空格。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371599.html
上一篇:查找不是數字??或預定義字串的值
下一篇:將標準輸入管道傳輸到cURL標頭
