Simple regular expression question

xuanbao · · 461 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am new to both Golang and regular expressions, so bear with me. I am trying to write a regular expression that only matches input in the following format: 1 capital letter between A and J followed by 1 or 2 digits. Think Battleship coordinates. i.e. D4 G10 B5</p> <p>Right now I have &#34;<sup>[A-J]{1}[0-9]{2}$&#34;</sup> but it doesn&#39;t work. I am using Go&#39;s regexp package. Any ideas?</p> <hr/>**评论:**<br/><br/>henderjon: <pre><p>test := &#34;D1 A67 J12 K9&#34; </p> <p>r := regexp.MustCompile(<code>([A-J]\d\d?)*</code>)</p> <p>fmt.Println(r.FindAllString(test, -1))</p> <p>gives me: [D1 A67 J12 ]</p> <p><a href="https://play.golang.org/p/nVt-Xq7AES" rel="nofollow">https://play.golang.org/p/nVt-Xq7AES</a></p> <p>[edit] If you really want to be strict about it:</p> <p>test := &#34;D1 A67 J12 K9 AA8 u769 uu876&#34; </p> <p>r := regexp.MustCompile(<code>(\b[A-J]\d\d?\b)*</code>)</p></pre>csjmp: <pre><p>This is perfect. Thank you!</p></pre>Forgd: <pre><p>[A-J]1?[0-9]</p> <p>I <em>think</em> that&#39;s what you&#39;re looking. </p></pre>bloodqc: <pre><p>You can test with tools like <a href="https://regex101.com/" rel="nofollow">https://regex101.com/</a></p></pre>Eggbert345: <pre><p>So there are couple answers here with the solution but I figured I should point out why their&#39;s works.</p> <p>Firstly, when you use the <code>{1}</code> concept, you&#39;re telling Go to match exactly that many characters. So <code>[0-9]{2}</code> would match two and only two digits (losing out on A1 B4 etc.). Also, the <code>$</code> character matches to the end of the string, so you would only match the characters that are right at the end of the string.</p> <p>Your regex actually works for coordinates with 2 digits at the end of the string, e.g. `&#34;A26&#34;.</p></pre>craictacular: <pre><p>&#34;[A-J][0-9]{1-2}&#34;</p></pre>izuriel: <pre><p>I&#39;d like to pitch in here, just because I don&#39;t feel like people have been explaining your problem and their solutions quite well enough. </p> <p>First off, the <em>only</em> problem in your original attempt was the <code>{2}</code> tacked onto the end of the regular expression. The reason is that <code>{...}</code> is a notation for regular expressions that say &#34;match the previous value some specific amount of times.&#34; These declarations are powerful, you can say things like &#34;match 1 or 2 times&#34; (what you probably wanted) <code>{1,2}</code>, or even &#34;match 1 or more&#34; <code>{1,}</code> (FYI, &#34;one or more&#34; has a special character <code>+</code>).</p> <p>So your original expression (with a simple modification) <code>[A-J]{1}[0-9]{1,2}$</code> gives you exactly what you want. </p> <p>One other thing to note though, while not an error, the <code>{1}</code> declaration is redundant. Simply specifying a character, character class or group is automatically only matched once. So just saying <code>[A-J]</code> means &#34;match a single letter between capital a and capital j.&#34;</p> <p>All in all your shorter regular expression is: <code>[A-J][0-9]{1,2}</code>.</p> <p>We can make one more &#34;optimization&#34; if you will. I quoted that because it&#39;s not really an optimization in the form of a speed and potentially a minor readability optimization if your reader is familiar with standard regular expression classes. The character class <code>[0-9]</code> is predefined as <code>\d</code>. It&#39;s negation, essentially <code>[^0-9]</code> is <code>\D</code>. The negation doesn&#39;t matter but we can use the class itself.</p> <p>So the final version of your original expression turns out to be: <code>[A-J]\d{1,2}</code>.</p> <p>We still get the same results as another comments example: <code>[D1 A67 J12 ]</code> (<a href="http://play.golang.org/p/BV2iQ8fLYh" rel="nofollow">http://play.golang.org/p/BV2iQ8fLYh</a>).</p> <p>Hope you have fun with more regular expressions!</p> <p><strong>Note</strong> I never really addressed it, but I dropped the the <code>$</code> from the end. You probably don&#39;t want that for reasons mentioned in another comment (due to <code>\n</code> not matching it unless in multiline mode).</p></pre>

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

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