我有一个标题为thisLine的字符串,我想在第一个整数之前删除所有字符.我可以使用该命令
regexpr("[0123456789]",thisLine)[1]
Run Code Online (Sandbox Code Playgroud)
确定第一个整数的位置.如何使用该索引拆分字符串?
flo*_*del 11
简短的回答:
sub('^\\D*', '', thisLine)
Run Code Online (Sandbox Code Playgroud)
哪里
^ 匹配字符串的开头\\D匹配任何非数字(它是相反的\\d)\\D* 尝试匹配尽可能多的连续非数字我的个人偏好,regexp完全跳过:
sub("^.*?(\\d)","\\1",thisLine)
#breaking down the regex
#^ beginning of line
#. any character
#* repeated any number of times (including 0)
#? minimal qualifier (match the fewest characters possible with *)
#() groups the digit
#\\d digit
#\\1 backreference to first captured group (the digit)
Run Code Online (Sandbox Code Playgroud)
你想要这个substring功能.
或者用于gsub一次性完成工作:
> gsub('^[^[:digit:]]*[[:digit:]]', '', 'abc1def')
[1] "def"
Run Code Online (Sandbox Code Playgroud)
您可能希望包含第一个数字,这可以通过捕获来完成:
> gsub('^[^[:digit:]]*([[:digit:]])', '\\1', 'abc1def')
[1] "1def"
Run Code Online (Sandbox Code Playgroud)
或者像Flodel和Alan所指出的那样,只需用空白替换"所有前导数字".见弗洛尔的答案.