1

I have some sequences in a string denoted by "@number" (/@\d/) I want to remove any redundant sequences, where @2 is followed by @2, I only want to remove them if another identical @number sequence is found directly after somewhere in the text, so for @2lorem@2ipsum the 2nd @2 is removed, but for @2lorem@1ipsum@2dolor nothing is removed because @1 is between the two @2 sequences.

"@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2"

becomes:

"@2randomtextrandomtextrandomtext@1bla@2bla2bla2
4

4 回答 4

1

您可以将其拆分为令牌:

my_string = "@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2"
tokens = my_string.scan /(@\d+)?((?:(?!@\d+).)*)/
#=> [["@2", "randomtext"], ["@2", "randomtext"], ["@2", "randomtext"], ["@1", "bla"], ["@2", "bla2"], ["@2", "bla2"]]

然后分块、映射和连接:

tokens.chunk{|x| x[0].to_s}.map{|n, v| [n, v.map(&:last)]}.join
#=> "@2randomtextrandomtextrandomtext@1bla@2bla2bla2"
于 2013-03-10T07:58:02.370 回答
1
"@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2".gsub /(?<=(@\d))([^@]*)\1/,'\2'
=> "@2randomtextrandomtextrandomtext@1bla@2bla2bla2"
于 2013-03-10T14:53:36.307 回答
0
my_string = "@2randomtext@2randomtext@2randomtext@1bla@2bla2@2bla2"

prev_sequence = String.new
penultimate_index = my_string.length - 2

for i in 0..penultimate_index
  if my_string[i] == '@'
    new_sequence = "@#{my_string[i+1]}"
    if new_sequence == prev_sequence
      my_string.slice!( i, 2 )
    else 
      prev_sequence = new_sequence
    end
  end
end


puts my_string
于 2013-03-10T01:41:10.467 回答
-1

容易...将您的字符串拆分为一个数组,然后比较紧随其后的数字。如果相同,请删除它/它们。复杂的(通过不是那么多)是你不能在循环遍历它们时从数组中删除条目......所以你需要做的是制作一个递归函数......这是伪:

-= 全局值 =-
Decalre StringArray 并将其设置为 OriginalString.SplitOn("@")

-= Method RemoveLeadingDuplicates =-
Declare Counter 为 StringArray 中的每个字符串
声明 RemoveIndex循环   if previous lead == current lead     Set RemoveIndex     break from loop   else     previous lead = current lead 将计数器增加 1 结束循环









如果 RemoveIndex 不为 null
  从数组中删除指定索引处的项目
  调用 RemoveLeadingDuplicates

返回

于 2013-03-10T01:28:56.917 回答