代码之家  ›  专栏  ›  技术社区  ›  krunal shah

如何删除“从字符串中引用”?[复制]

  •  0
  • krunal shah  · 技术社区  · 16 年前


    ruby code for modifying outer quotes on strings?

    这是我的绳子:

    msgid """We couldn't set up that account, sorry.  Please try again, or contact an ""admin (link is above)."
    

    我想删除所有的双引号,除了第一个和最后一个。

    4 回复  |  直到 9 年前
        1
  •  1
  •   Michael Kohl    16 年前

    这是假设您的字符串始终是上面显示的“msgid…”格式,并且您的输入输出是“msgid”text here“':

    >> str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
    => "msgid "We couldn't set up that account, sorry.  Please try again or contact an admin (link is above).""
    >> puts str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
    msgid "We couldn't set up that account, sorry.  Please try again or contact an admin (link is above)."
    
        2
  •  1
  •   Tony Eichelberger    16 年前

    这里有一个选项,只要你知道你总是希望在开头和结尾都有报价。

    假设x拿着你想要操纵的刺。

    x = '"' + x.gsub('"', '') + '"'
    
        3
  •  0
  •   the Tin Man    16 年前

    msg = %q{msgid """We couldn't set up that account, sorry.  Please try again, or contact an ""admin (link is above)."}
    print 'msgid "' << msg.sub('msgid ', '').gsub('"', '') << '"'
    # >> msgid "We couldn't set up that account, sorry.  Please try again, or contact an admin (link is above)."
    

    如果“msgid”在每一行前面,则将其删除,通过剥离双引号来处理双引号,然后将“msgid”放回原处,并再次添加所需的前导和尾随双引号。

        4
  •  0
  •   DarthJDG chetan borse    15 年前

    由于双引号的字符串分隔符,msgid实际上是3个字符串。所以你可以:

    msgid = ""<<"We couldn't set up that account, sorry. Please try again, or contact an "<<"admin (link is above)."
    

    x = %q{}
      msgid = """We couldn't set up that account, sorry. Please try again, or contact an ""admin (link is above)."
      x << msgid
      x.gsub!('"', '')
      msgid = ""
      msgid << x
    # Check
      puts x
    
    推荐文章