问题描述
str_vec <- c("1 9.7 1 0.8","7.6 7.5","3.7 13.5","8.6 1 5.8")
我需要删除数字中的空格。即,我希望此向量的第一个和最后一个字符串为:
"19.7 10.8"
"8.6 15.8"
我看到Java语言有another similar post。看起来很有用,但我无法将其翻译为R。
解决方法
我不确定这是否是你所追求的
lapply(
strsplit(str_vec,split = "(?<=\\.\\d)\\s",perl = TRUE),function(x) gsub("\\s","",x)
)
给出
[[1]]
[1] "19.7" "10.8"
[[2]]
[1] "7.6" "7.5"
[[3]]
[1] "3.7" "13.5"
[[4]]
[1] "8.6" "15.8"
,
或者:
library(stringr)
str_vec <- c("1 9.7 1 0.8","7.6 7.5","3.7 13.5","8.6 1 5.8")
want <- str_remove_all(str_vec,"\\s")
want <- str_replace(want,"(\\d+\\.\\d)(\\d+\\.\\d)","\\1 \\2")
want
#"19.7 10.8" "7.6 7.5" "3.7 13.5" "8.6 15.8"
,
您可以使用
Private Sub copyCodeToClipboard(codeType As enCodeSnippets)
Dim codeText
codeText = getCodeText(codeType)
copyToClipboard codeText
End Sub
Sub copyToClipboard(textI)
'GLOBAL CALLER TO COPY TEXT TO CLIPBOARD
Dim DObj As New DataObject
DObj.SetText (textI)
DObj.PutInClipboard
End Sub
Sub pasteClipboard()
Application.SendKeys "^v",True
End Sub
正则表达式详细信息
-
library(stringr) x <- c("1 9.7 1 0.8","8.6 1 5.8") str_replace_all(x,"\\d(?:\\s*\\d)*\\.\\d+",function(z) str_replace_all(z,"\\s+","")) # => [1] "19.7 10.8" "7.6 7.5" "3.7 13.5" "8.6 15.8"
-一个数字 -
\d
-0次或更多次出现0个或更多个空格,后跟一个数字 -
(?:\s*\d)*
-一个点 -
\.
-一个或多个数字。
仅使用\d+
就可以从每个匹配项中删除所有空格。
不知道在R中将如何完成此操作,但这是在python中完成的方式
static IEnumerable<string> Suits() {
yield return "clubs";
yield return "diamonds";
yield return "hearts";
yield return "spades";
}
static IEnumerable<string> Ranks() {
yield return "two";
yield return "three";
yield return "four";
yield return "five";
yield return "six";
yield return "seven";
yield return "eight";
yield return "nine";
yield return "ten";
yield return "jack";
yield return "queen";
yield return "king";
yield return "ace";
}
链接到Regex101