字符串错误:未知的转义序列:/

huangapple go评论79阅读模式
英文:

String Error : unknown escape sequence: /

问题

我有一个类似这样的字符串:

data-url==\"\/entity\/id\/2439918080\"

如果我在这个字符串上使用Println,会抛出一个错误unknown escape sequence: /

请参考http://play.golang.org/p/IbURcPc_gZ(注释掉第一条语句就可以工作)

然而,如果我将\/替换为\u002F,它就可以完美地打印出来。实际的输入数据中充满了这样编码的URL。有没有办法可以在不改变它为\u002F的情况下打印出来?

这是来自JSON的实际字符串:

<div class="card2 media-container portrait" data-card2-name="summary_large_image">
    
    <div class="js-macaw-cards-iframe-container"
  data-src="\/i\/entity\/tfw\/v1\/3298718080?cardname=summary_large_image&earned=true"
  data-autoplay-src="\/i\/cards\/tfw\/v1\/632987845439918080?cardname=summary_large_image&earned=true"
  data-card-name="summary_large_image"
  data-card-url="http:\/\/t.co\/QqWvUQMs5B"
  data-publisher-id="6721522"
  data-creator-id=""
  data-amplify-content-id=""
  data-amplify-playlist-url=""
  data-full-card-iframe-url="\/i\/cards\/tfw\/v1\/632987845439918080?cardname=summary_large_image&earned=true"
  data-has-autoplayable-media="false">
<\/div>

<\/div>



        <div class="entities-media-container js-media-container" style="min-height:0px">
        <\/div>

  <div class="machine-translated-container"><\/div>
    <div class="stats-container stats-container ">
    <\/div>

  <div class="client-and-actions">
  <span class="metadata">
    <span>2:50 PM - 16 Aug 2015<\/span>

       · <a class="permalink-link js-permalink js-nav" href="\/intifada\/status\/632987845439918080"  tabindex="-1">Details<\/a>
    


  <\/span>
<\/div>


<\/div>
\"\n\n\n\n\n\n\n\n data-you-follow=\"true\"\n data-follows-you=\"false\"\n data-you-block=\"false\"\n\n\n\n\n\u003e\n\n\n    \u003cdiv class=\"context\"\u003e\n      \n      \n    \u003c\/div\u003e\n    \u003cdiv class=\"content\"\u003e\n\n      \n      \u003cdiv class=\"stream-item-header\"\u003e\n 

谢谢。

英文:

I have an incoming string that looks like this

data-url==\"\/entity\/id\/2439918080\"

If I Println on this string, it throws an error unknown escape sequence: /

See http://play.golang.org/p/IbURcPc_gZ (comment the first statement and it works)

However if I replace \/ with \u002F it prints perfectly. The actual incoming data is full of such encoded urls. Is there anyway I can print it without having to change it to \u002F ?

Here is the actual string that comes from json

  <div class="card2 media-container portrait" data-card2-name="summary_large_image">
    
    <div class="js-macaw-cards-iframe-container"
  data-src="\/i\/entity\/tfw\/v1\/3298718080?cardname=summary_large_image&earned=true"
  data-autoplay-src="\/i\/cards\/tfw\/v1\/632987845439918080?cardname=summary_large_image&earned=true"
  data-card-name="summary_large_image"
  data-card-url="http:\/\/t.co\/QqWvUQMs5B"
  data-publisher-id="6721522"
  data-creator-id=""
  data-amplify-content-id=""
  data-amplify-playlist-url=""
  data-full-card-iframe-url="\/i\/cards\/tfw\/v1\/632987845439918080?cardname=summary_large_image&earned=true"
  data-has-autoplayable-media="false">
<\/div>

<\/div>



        <div class="entities-media-container js-media-container" style="min-height:0px">
        <\/div>

  <div class="machine-translated-container"><\/div>
    <div class="stats-container stats-container ">
    <\/div>

  <div class="client-and-actions">
  <span class="metadata">
    <span>2:50 PM - 16 Aug 2015<\/span>

       · <a class="permalink-link js-permalink js-nav" href="\/intifada\/status\/632987845439918080"  tabindex="-1">Details<\/a>
    


  <\/span>
<\/div>


<\/div>
\"\n\n\n\n\n\n\n\n data-you-follow=\"true\"\n data-follows-you=\"false\"\n data-you-block=\"false\"\n\n\n\n\n\u003e\n\n\n    \u003cdiv class=\"context\"\u003e\n      \n      \n    \u003c\/div\u003e\n    \u003cdiv class=\"content\"\u003e\n\n      \n      \u003cdiv class=\"stream-item-header\"\u003e\n 

Thanks.

答案1

得分: 1

如果你只是想获取一个有效的字符串:

package main

import "fmt"
import "strings"

func main() {
        
    s := `data-url==\"/entity/id/2439918080\"`
    s = strings.Replace(s,"\\","",-1)
    println(s)
	
	//Replace \/ with \u002F works fine
	fmt.Println("data-url==\"\u002Fentity\u002Fid\u002F2439918080\"")
}
英文:

If you are just trying to get a valid string :

package main

import "fmt"
import("strings")

func main() {
        
    s := `data-url==\"\/entity\/id\/2439918080\"`
    s = strings.Replace(s,"\\","",-1)
    println(s)
	
	//Replace \/ with \u002F works fine
	fmt.Println("data-url==\"\u002Fentity\u002Fid\u002F2439918080\"")
}

huangapple
  • 本文由 发表于 2015年9月1日 23:01:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/32334950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定