[英]regular expression help in R
I have data that looks like this, where the negative sign is a suffix at the end of the numeric. 我有看起来像这样的数据,其中负号是数字末尾的后缀。
"general_amount"
"0000000441244"
"0000000127769-"
"0000000043819"
"0000000522600-"
Can someone help me find a regular expression to produce the desired output below. 有人可以帮我找到一个正则表达式,以在下面产生所需的输出。
"general_amount"
441244
-127769
43819
-522600
sub('^0*([^-]*)(-?)$', '\\2\\1', x)
## [1] "general_amount" "441244" "-127769" "43819" "-522600"
^0*
matches all leading 0
characters. ^0*
匹配所有前导0
字符。
[^-]*
matches all non- -
characters. [^-]*
匹配所有非-
字符。
-?
matches zero or one -
character. 匹配零个或一个-
字符。
Finally, the $
matches the end of the string. 最后, $
匹配字符串的结尾。
The middle two pieces are captured with ()
, as \\\\1
and \\\\2
, and printed in reverse order. 中间的两个片断被()
捕获为\\\\1
和\\\\2
,并以相反的顺序打印。
Using gsub , with another idea. 使用gsub ,还有另一个想法。
The idea is to divide the input into 3 elements 想法是将输入分为3个元素
find the '-' 1 or zero times : (-?)" 找到“-” 1或零次:(-?)”
as.numeric(gsub("(^0+)([0-9]+)(-?)","\\\\3\\\\2",tt)) [1] 441244 -127769 43819 -522600
Dude it took me 3 hours to find answer to your question 老兄,我花了3个小时才找到问题的答案
sed -re 's/[^a-zA-Z0-9]0+([0-9]+)(-?)/\2\1/g' anyfile.txt
But in the end i did it. 但是最后我做到了。 May have some short coming but i got it nearly 可能会有一些短暂的机会,但我快到了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.