Regex Ruby正则表达式从只包含一个数字的字符串中提取一个数字,并修剪逗号后的部分
我有一个使用正则表达式从字符串中提取数字的方法,如下所示:Regex Ruby正则表达式从只包含一个数字的字符串中提取一个数字,并修剪逗号后的部分,regex,ruby,numbers,currency,number-formatting,Regex,Ruby,Numbers,Currency,Number Formatting,我有一个使用正则表达式从字符串中提取数字的方法,如下所示: def格式 str=“R$10.000,00+福利” str.split(/[^\d]/).join 结束 它返回-->1000000。我需要修改regex以返回10000,删除逗号后的零。您可以使用 str.gsub(/(?<=\d),\d+|\D/, '') str.gsub(/(?这里是一个略长但可能更简单、更容易理解的解决方案。你可以用它来替代Wiktor Stribiżew精辟的回答和Cary Swoveland非常
def格式
str=“R$10.000,00+福利”
str.split(/[^\d]/).join
结束
它返回-->1000000
。我需要修改regex以返回10000
,删除逗号后的零。您可以使用
str.gsub(/(?<=\d),\d+|\D/, '')
str.gsub(/(?这里是一个略长但可能更简单、更容易理解的解决方案。你可以用它来替代Wiktor Stribiżew精辟的回答和Cary Swoveland非常彻底和完整的回答。请注意,我的回答可能对某些人不起作用(更复杂)字符串,如下面Cary的评论中所述
此处两次应用于输入字符串:
gsub(/^.*(\d+[\d.]*).$/,'\1')
:抓取10.000
部分。
^
是字符串的开头。
*?
是任何字符重复0次或更多次,非贪婪(即最小次数)。
(\d[\d.]*)
是后跟数字或文字点(
)的任何数字。括号捕获该数字并将其放入第一个捕获组(稍后用作替换字符串)。
*
是任何字符重复0次或更多次,贪婪(即尽可能多)。
$
是字符串的结尾。
因此,我们将整个字符串替换为第一个捕获的组:'\1'
,即10.000
。请记住在\1
周围使用单引号,否则将其转义两次:“\\1”
gsub(/[.]/,“”)
:删除字符串中的所有文字点(
)
请注意,此代码对许多类似的字符串进行了预期的替换(但没有更高级的字符串,例如将001
保持原样):
输出:
R$ 10.000,00 + Benefits => 10000
R$ 0,00 + Benefits => 0
R$ .001,00 + Benefits => 001
. 10.000,00 + Benefits => 10000
以下子字符串均不匹配,因为它们的格式无效:“4.00,10”
,3.30005,00”
,“6.700”
,“6”
,“6,0”
,“00,20”
,“6001”
,“5.122,00”
(最后一个原因是它前面没有“$R”
,,
正则表达式可以在自由间距模式(/x
)下编写,以使其能够自文档化
R = /
(?: # begin non-capture group
(?<=\bR\$) # positive lookbehind asserts match is preceded by 'R$'
# that is preceded by a word break
| # or
(?<=\bR\$\ ) # positive lookbehind asserts match is preceded by 'R$ '
# that is preceded by a word break
) # end non-capture group
(?<= # begin negative lookbehind
$R[ ]) # asserts that match is preceded by a space
(?: # begin non-capture group
0 # match zero
| # or
[1-9] # match a digit other than zero
\d{0,2} # match 0-2 digits
(?:\.\d{3}) # match '.' followed by three digits in a non-capture group
* # execute preceding non-capture group 0+ times
) # end non-capture group
,\d{2} # match ',' followed by two digits
(?!\d) # negative lookahead asserts match is not followed by a digit
/x
R=/
(?:#开始非捕获组
(?str.gsub(/)(?@WiktorStribiżew你是一个正则表达式向导!请你解释一下?str.gsub(/)如果字符串中有多个数字怎么办?str=“乔付了10.000,00美元,但简以7.900,00美元得到了更好的交易”
删除所有十进制数字和非十进制数字会给您留下100007900
。或者这种情况永远不会发生吗?如果@3limin4t0r的评论不明显,那么如果字符串中有任何其他数字,而不仅仅是美元金额的表示,则会出现问题:“10月7日乔支付了10.000,00美元”。拆分(/[^\d]/)。加入=>“71000000
”10月7日,乔支付了10.000,00雷亚尔。”.gsub(/(?“710000”
@CarySwoveland字符串OP只包含一个数值。”10月7日,乔支付了10.000,00雷亚尔。”.gsub(/^.*(\d+[\d.]*).$/,'\1')。gsub(/[.]/,''.]/,'')=>“7”
@CarySwoveland感谢您指出我答案中某些字符串的错误解析。
R$ 10.000,00 + Benefits => 10000
R$ 0,00 + Benefits => 0
R$ .001,00 + Benefits => 001
. 10.000,00 + Benefits => 10000
str = "R$ 10.000,00 R$1.200.000,03 R$ 0,09 R$ 4.00,10 R$ 3.30005,00 R$ 6.700 R$ 6, R$ 6,0 R$ 00,20 R$6,001 US$ 5.122,00 Benefits"
R = /(?:(?<=\bR\$)|(?<=\bR\$ ))(?:0|[1-9]\d{0,2}(?:\.\d{3})*),\d{2}(?!\d)/
str.scan(R).map { |s| s.delete('.') }
#=> ["10000,00", "1200000,03", "0,09"]
R = /
(?: # begin non-capture group
(?<=\bR\$) # positive lookbehind asserts match is preceded by 'R$'
# that is preceded by a word break
| # or
(?<=\bR\$\ ) # positive lookbehind asserts match is preceded by 'R$ '
# that is preceded by a word break
) # end non-capture group
(?<= # begin negative lookbehind
$R[ ]) # asserts that match is preceded by a space
(?: # begin non-capture group
0 # match zero
| # or
[1-9] # match a digit other than zero
\d{0,2} # match 0-2 digits
(?:\.\d{3}) # match '.' followed by three digits in a non-capture group
* # execute preceding non-capture group 0+ times
) # end non-capture group
,\d{2} # match ',' followed by two digits
(?!\d) # negative lookahead asserts match is not followed by a digit
/x