使用正则表达式将字符串拆分为数组但不删除匹配项
Split string into array with regex but without removing the matches
在python我能做到:
import re
re.split('(o)', 'hello world')
并得到:
['hell', 'o', ' w', 'o', 'rld']
与crystal:
"hello world".split(/(o)/)
我得到:
["hell", " w", "rld"]
但我想像 python 示例中那样将匹配项保留在数组中。可能吗?
这是刚刚添加的,请参阅 this issue。
在发布之前,您可以使用环视表达式进行欺骗:
"hello world".split(/(?<=o)|(?=o)/) #=> ["hell", "o", " w", "o", "rld"]
在python我能做到:
import re
re.split('(o)', 'hello world')
并得到:
['hell', 'o', ' w', 'o', 'rld']
与crystal:
"hello world".split(/(o)/)
我得到:
["hell", " w", "rld"]
但我想像 python 示例中那样将匹配项保留在数组中。可能吗?
这是刚刚添加的,请参阅 this issue。
在发布之前,您可以使用环视表达式进行欺骗:
"hello world".split(/(?<=o)|(?=o)/) #=> ["hell", "o", " w", "o", "rld"]