如何拆分包含分隔符和转义分隔符的字符串? [英] How to split a string containing both delimiter and the escaped delimiter?

查看:91
本文介绍了如何拆分包含分隔符和转义分隔符的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串分隔符是 ;.分隔符在字符串中转义为 \;.例如,

My string delimiter is ;. Delimiter is escaped in the string as \;. E.g.,

irb(main):018:0> s = "a;b;;d\\;e"
=> "a;b;;d\\;e"
irb(main):019:0> s.split(';')
=> ["a", "b", "", "d\\", "e"]

有人可以建议我使用正则表达式,因此 split 的输出将是 ["a", "b", "", "d\\;e"] 吗?我使用的是 Ruby 1.8.7

Could someone suggest me regex so the output of split would be ["a", "b", "", "d\\;e"]? I'm using Ruby 1.8.7

推荐答案

1.8.7 在没有 Oniguruma(可能会被编译)的情况下没有负面回溯.

1.8.7 doesn't have negative lookbehind without Oniguruma (which may be compiled in).

1.9.3;耶:

> s = "a;b;c\\;d"
=> "a;b;c\\;d"
> s.split /(?<!\\);/
=> ["a", "b", "c\\;d"]

1.8.7 with Oniguruma 不提供微不足道的拆分,但您可以获得匹配偏移量并以这种方式分离子字符串.我想有更好的方法可以做到这一点,我不记得了:

1.8.7 with Oniguruma doesn't offer a trivial split, but you can get match offsets and pull apart the substrings that way. I assume there's a better way to do this I'm not remembering:

> require 'oniguruma'
> re = Oniguruma::ORegexp.new "(?<!\\\\);"
> s = "hello;there\\;nope;yestho"
> re.match_all s
=> [#<MatchData ";">, #<MatchData ";">]
> mds = re.match_all s
=> [#<MatchData ";">, #<MatchData ";">]
> mds.collect {|md| md.offset}
=> [[5, 6], [17, 18]]

其他选项包括:

  • ; 进行拆分并对结果进行后处理以查找尾随 \\
  • 执行一个逐个字符的循环并保持一些简单的状态,然后手动拆分.
  • Splitting on ; and post-processing the results looking for trailing \\, or
  • Do a char-by-char loop and maintain some simple state and just split manually.

这篇关于如何拆分包含分隔符和转义分隔符的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆