gpt4 book ai didi

r - 在模式发生后拆分字符串

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

我有以下字符串向量。它包含两个元素。每个元素都由两个折叠的短语组成。

strings <- c("This is a phrase with a NameThis is another phrase",
"This is a phrase with the number 2019This is another phrase")

我想为向量中的每个元素拆分这些短语。我一直在尝试类似的东西:
library(stringr)

str_split(strings, "\\B(?=[a-z|0-9][A-Z])")

这几乎给了我我正在寻找的东西:
[[1]]
[1] "This is a phrase with a Nam" "eThis is another phrase"

[[2]]
[1] "This is a phrase with the number 201" "9This is another phrase"

我想在模式之后进行拆分,但无法弄清楚如何做到这一点。

我想我已经接近解决方案了,希望得到任何帮助。

最佳答案

您需要匹配大写字母之前的位置,而不是初始短语最后一个字母之前的位置(您需要的位置前一个字符)。您可能只是将非单词边界与大写字母的前瞻匹配:

str_split(strings, "\\B(?=[A-Z])")

如果短语可以包含前导大写字母,但小写字母开始后不包含任何大写字母,您也可以使用lookbehind将它们拆分为数字或小写字母。这次不需要非字边界:
strings <- c("SHOCKING NEWS: someone did somethingThis is another phrase",
"This is a phrase with the number 2019This is another phrase")
str_split(strings, "(?<=[a-z0-9])(?=[A-Z])")

关于r - 在模式发生后拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51958524/

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