Need help in formatting

xuanbao · · 429 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am starting to learn go and got into a problem(challenge). I have to format text.</p> <p>Input will be like: <code>api=&lt;http://example.com/first/api:latest|example.com/first/api:latest&gt;</code></p> <p>And I want output like this:</p> <p><code>api=example.com/first/api:latest</code></p> <p>i.e. strip gt &amp; lt signs, <code>http</code> OR text before <code>|</code> and <code>&gt;</code> sign. </p> <p>Context: Slack bot. It interprets text with dot as link and sends it in <code>&lt;..|..&gt;</code> format. If you are not clear, please feel free to comment.</p> <p>I was seeing <code>net/url</code> but I think that would be not of big help.</p> <p>Any help, comment, resource I should see will be highly appreciated. Thanks.</p> <hr/>**评论:**<br/><br/>titpetric: <pre><p><a href="https://play.golang.org/p/snse0sObOf" rel="nofollow">https://play.golang.org/p/snse0sObOf</a></p> <ol> <li>match text between &lt;...&gt;</li> <li>split by |</li> <li>take second part (right of |)</li> <li>trim length by one (getting rid of &gt;)</li> <li>replace match with the cleaned value</li> </ol> <p>Should be enough as a base for whatever you&#39;re doing from here out.</p></pre>procipher: <pre><p>Bit generalization would be to take nearest <code>&lt;</code> and <code>&gt;</code> from <code>|</code> ... case: <a href="https://play.golang.org/p/3cCTpDRWij" rel="nofollow">https://play.golang.org/p/3cCTpDRWij</a> . </p> <p>Anyway, thank you very much.</p></pre>titpetric: <pre><p>Easy enough fix, one character: <a href="https://play.golang.org/p/ydHwwFxOI_" rel="nofollow">https://play.golang.org/p/ydHwwFxOI_</a> (regex changes from <code>[^&gt;]</code> to <code>[^&lt;&gt;]</code>). There&#39;s probably edge cases abound...</p></pre>procipher: <pre><p>Yep. Thanks. btw, good that it also works if there is <code>|</code> before or after the link stuff :)</p></pre>

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

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