我正在尝试编写一个gsub表达式,该表达式将用连字符(–)替换连字符(-),其中连字符以数字开头。基本上是因为我想将日期段显示为1978 – 1980,而不是出现在我的数据源中的1978-1980。
连字符和尾码看起来很像我,所以我想具体些,并使用Unicode字符表示尾码为U + 2013,而连字符为U + 002D。
作为测试,我想转换:
"america-the-beautiful. 1760-about 1780"
至
"america-the-beautiful. 1760 – about 1780"
与test_string = "america-the-beautiful. 1760-about 1780"
我已经确认正则表达式正确地识别了仅带数字的连字符,并且gsub替换为Endash的占位符。
test_string.gsub(/(\d)-/, '\1 endash_placeholder ')
=>“美丽的美国。1760年endash_placeholder约1780年”
我正在努力删除连字符和endash_placeholder并使用实际的unicode字符。
为了解决这个问题Ruby Output Unicode Character,我使用了许多SO问题。
在irb中,我可以使用puts "\u{2013}"
我尝试将gsub表达式修改为test_string.gsub(/(\d)-/, '\1 \u{2013} ')
=> "america-the-beautiful. 1760 \\u{2013} ca. 1780"
我还尝试过用双引号将unicode引用:
test_string.gsub(/(\d)-/, "\1 \u{2013} ")
=> "america-the-beautiful. 176\u0001 – ca. 1780"
为了在gsub表达式中使用特定的unicode字符代码,我缺少什么?