我有一个字符串:
str = "AB asdfsa"
匹配正则表达式:
/^([A-Z][A-Z][[:space:]])[A-Z]/i
如何获取括号之间匹配的部分,然后将其移动到字符串的末尾?上面我想要的输出是一个字符串:
"asdfsa AB"
同样,如果有一个字符串:
"BC ffff eeee"
我希望结果是:
"ffff eeee BC"
我认为match?
关键字允许我提取该部分,但它只返回true
/ false
:
str.match?(/^([A-Z][A-Z])[[:space:]][A-Z]/i) # => true
答案 0 :(得分:4)
我建议使用与use this
android:ellipsize="end"
android:maxLines="1"
android:maxLength="25"
一起使用的单一正则表达式解决方案:
sub
请参阅Ruby demo
<强>详情:
def swap_parts(s)
s.sub(/\A([A-Z][A-Z])([[:space:]]+)([A-Z].*)/im, '\3\2\1')
end
swap_parts('AB asdfsa') # => asdfsa AB
swap_parts('BC ffff eeee') # => ffff eeee BC
- 字符串的开头\A
(或([A-Z][A-Z])
) - (第1组)2个ASCII字母(不区分大小写,因为使用了([A-Z]{2})
修饰符)i
- (第2组)1个或多个空格([[:space:]]+)
- ( Group 3 )一个ASCII字母和任何0+个字符(自([A-Z].*)
修饰符传递后,m
现在将换行符匹配为好。替换模式以相反的顺序插入捕获的部分:首先使用第3组,然后附加第2组值,最后进入第3组。
答案 1 :(得分:2)
你很亲密。将match
替换为split
,然后致电reverse.join(' ')
:
str = 'AB sdjfiosjdf'
str.split(/^([A-Z][A-Z])[[:space:]]/i).reverse.join(' ')
=> 'sdjfiosjdf AB'
答案 2 :(得分:2)
正则表达式很好,但它们无法解决所有问题。如果你总是按字词分割并旋转生成的数组,那么这样做会更简单快捷,split.rotate
:
[
'AB asdfsa',
'BC ffff eeee'
].map { |s| s.split.rotate.join(' ') }
# => ["asdfsa AB", "ffff eeee BC"]
或者:
[
'AB asdfsa',
'BC ffff eeee'
].map { |s| s.split(' ', 2).rotate.join(' ') }
# => ["asdfsa AB", "ffff eeee BC"]
或者:
[
'AB asdfsa',
'BC ffff eeee'
].map { |s| s.split(' ', 2).reverse.join(' ') }
# => ["asdfsa AB", "ffff eeee BC"]
对于长字符串, split
将比split(' ', 2)
慢,因为第一个字符串将在整个字符串上分割,这可以创建一个长数组。后者只会找到第一次出现的空白然后返回:
require 'fruity'
str = ['AB', ['asdfsa'] * 100].join(' ')
compare do
split_rotate { str.split.rotate.join(' ') }
split_2_rotate { str.split(' ', 2).rotate.join(' ') }
split_2_reverse { str.split(' ', 2).reverse.join(' ') }
end
# >> Running each test 4096 times. Test will take about 3 seconds.
# >> split_2_reverse is similar to split_2_rotate
# >> split_2_rotate is faster than split_rotate by 22x ± 1.0
正则表达式不会很快运行,因为他们需要做更多的工作:
require 'fruity'
str = ['AB', ['asdfsa'] * 100].join(' ')
compare do
split_rotate { str.split.rotate.join(' ') }
split_2_rotate { str.split(' ', 2).rotate.join(' ') }
split_2_reverse { str.split(' ', 2).reverse.join(' ') }
victor { str.sub(/\A([A-Z][A-Z])([[:space:]]+)([A-Z].*)/im, '\3\2\1') }
whodini9 { str.split(/^([A-Z][A-Z])[[:space:]]/i).reverse.join(' ') }
engineersmnky { /^(?<first_two>[A-Z]{2})[[:space:]](?<rest>.+)/i.match(str).captures.reverse.join(' ') }
end
# >> Running each test 4096 times. Test will take about 4 seconds.
# >> split_2_rotate is similar to split_2_reverse
# >> split_2_reverse is faster than victor by 7x ± 1.0
# >> victor is similar to whodini9 (results differ: asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa AB vs asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa AB )
# >> whodini9 is similar to engineersmnky (results differ: asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa AB vs asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa asdfsa AB)
# >> engineersmnky is faster than split_rotate by 3x ± 1.0
即使在短字符串上,由于正则表达式引擎的启动时间,正则表达式也有缺点。
答案 3 :(得分:1)
我会尝试像
这样的东西def swap_first_two(str)
reg_exp = /^(?<first_two>[A-Z]{2})[[:space:]](?<rest>.+)/i
reg_exp.match(str).captures.reverse.join(' ')
end
这应该适用于OP用例和注释用例,例如
swap_first_two('AB asdfsa')
#=> "asdfsa AB"
swap_first_two('AB OAK CREEK')
#=> "OAK CREEK AB"
使用Regexp#match
和MatchData#captures
而不是String#split
答案 4 :(得分:0)
将字符串转换为数组,操作数组然后将其转换回字符串对我来说总是最后的选择。我没有充分的理由;皈依舞似乎对我来说有点野蛮。我认为如果只通过操纵字符串可以完成某些事情,那通常就是要走的路。我把正则表达式作为给定的:
r = /^([A-Z][A-Z][[:space:]])[A-Z]/i
你可以写:
str = "AB asdfsa"
str.sub(r, '') << ' ' << $1[0..-2]
#=> "sdfsa AB"
或
n = str.match(r).end(0)
#=> 4
"%s %s" % [str[n..-1], str[0,n-2]]
#=> "sdfsa AB"
请注意,这些适用于以空格结尾的任何捕获组。 @ Wiktor的回答也只是操纵字符串。