Need help in formatting

xuanbao · 2017-10-05 12:00:08 · 628 次点击    
这是一个分享于 2017-10-05 12:00:08 的资源,其中的信息可能已经有所发展或是发生改变。

I am starting to learn go and got into a problem(challenge). I have to format text.

Input will be like: api=<http://example.com/first/api:latest|example.com/first/api:latest>;

And I want output like this:

api=example.com/first/api:latest

i.e. strip gt & lt signs, http OR text before | and > sign.

Context: Slack bot. It interprets text with dot as link and sends it in <..|..> format. If you are not clear, please feel free to comment.

I was seeing net/url but I think that would be not of big help.

Any help, comment, resource I should see will be highly appreciated. Thanks.


评论:

titpetric:

https://play.golang.org/p/snse0sObOf

  1. match text between <...>
  2. split by |
  3. take second part (right of |)
  4. trim length by one (getting rid of >)
  5. replace match with the cleaned value

Should be enough as a base for whatever you're doing from here out.

procipher:

Bit generalization would be to take nearest < and > from | ... case: https://play.golang.org/p/3cCTpDRWij .

Anyway, thank you very much.

titpetric:

Easy enough fix, one character: https://play.golang.org/p/ydHwwFxOI_ (regex changes from [^>] to [^<>]). There's probably edge cases abound...

procipher:

Yep. Thanks. btw, good that it also works if there is | before or after the link stuff :)


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

628 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传