通过正则表达式拆分特定字符串

时间:2021-10-27 21:38:17

i am trying to get an array that contain of aaaaa,bbbbb,ccccc as split output below.

我试图得到一个包含aaaaa,bbbbb,ccccc作为分割输出的数组。

a_string = "aaaaa[x]bbbbb,ccccc";
split_output a_string.split.split(%r{[,|........]+})

what supposed i put as replacement of ........ ?

我应该把它作为........的替代品?

2 个解决方案

#1


2  

No need for a regex when it's just a literal:

当它只是一个文字时,不需要正则表达式:

irb(main):001:0> a_string = "aaaaa[x]bbbbb"
irb(main):002:0> a_string.split "[x]"
=> ["aaaaa", "bbbbb"]

If you want to split by "open bracket...anything...close bracket" then:

如果你想通过“打开括号...任何...关闭括号”进行拆分,那么:

irb(main):003:0> a_string.split /\[.+?\]/
=> ["aaaaa", "bbbbb"]

Edit: I'm still not sure what your criteria is, but let's guess that what you are really doing is looking for runs of 2-or-more of the same character:

编辑:我仍然不确定你的标准是什么,但我们猜测你真正在做的是寻找2个或更多相同角色的运行:

irb(main):001:0> a_string = "aaaaa[x]bbbbb,ccccc"
=> "aaaaa[x]bbbbb,ccccc"
irb(main):002:0> a_string.scan(/((.)\2+)/).map(&:first)
=> ["aaaaa", "bbbbb", "ccccc"]

Edit 2: If you want to split by either the of the literal strings "," or "[x]" then:

编辑2:如果要分割文字字符串“,”或“[x]”,则:

irb(main):003:0> a_string.split /,|\[x\]/
=> ["aaaaa", "bbbbb", "ccccc"]

The | part of the regular expression allows expressions on either side to match, and the backslashes are needed since otherwise the characters [ and ] have special meaning. (If you tried to split by /,|[x]/ then it would split on either a comma or an x character.)

|正则表达式的一部分允许两侧的表达式匹配,并且需要反斜杠,否则字符[和]具有特殊含义。 (如果你试图用/,| [x] /拆分它会分裂为逗号或x字符。)

#2


1  

no regex needed, just use "[x]"

不需要正则表达式,只需使用“[x]”

#1


2  

No need for a regex when it's just a literal:

当它只是一个文字时,不需要正则表达式:

irb(main):001:0> a_string = "aaaaa[x]bbbbb"
irb(main):002:0> a_string.split "[x]"
=> ["aaaaa", "bbbbb"]

If you want to split by "open bracket...anything...close bracket" then:

如果你想通过“打开括号...任何...关闭括号”进行拆分,那么:

irb(main):003:0> a_string.split /\[.+?\]/
=> ["aaaaa", "bbbbb"]

Edit: I'm still not sure what your criteria is, but let's guess that what you are really doing is looking for runs of 2-or-more of the same character:

编辑:我仍然不确定你的标准是什么,但我们猜测你真正在做的是寻找2个或更多相同角色的运行:

irb(main):001:0> a_string = "aaaaa[x]bbbbb,ccccc"
=> "aaaaa[x]bbbbb,ccccc"
irb(main):002:0> a_string.scan(/((.)\2+)/).map(&:first)
=> ["aaaaa", "bbbbb", "ccccc"]

Edit 2: If you want to split by either the of the literal strings "," or "[x]" then:

编辑2:如果要分割文字字符串“,”或“[x]”,则:

irb(main):003:0> a_string.split /,|\[x\]/
=> ["aaaaa", "bbbbb", "ccccc"]

The | part of the regular expression allows expressions on either side to match, and the backslashes are needed since otherwise the characters [ and ] have special meaning. (If you tried to split by /,|[x]/ then it would split on either a comma or an x character.)

|正则表达式的一部分允许两侧的表达式匹配,并且需要反斜杠,否则字符[和]具有特殊含义。 (如果你试图用/,| [x] /拆分它会分裂为逗号或x字符。)

#2


1  

no regex needed, just use "[x]"

不需要正则表达式,只需使用“[x]”