<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 "<sup>[A-J]{1}[0-9]{2}$"</sup> but it doesn't work. I am using Go's regexp package. Any ideas?</p>
<hr/>**评论:**<br/><br/>henderjon: <pre><p>test := "D1 A67 J12 K9" </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 := "D1 A67 J12 K9 AA8 u769 uu876" </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's what you'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's works.</p>
<p>Firstly, when you use the <code>{1}</code> concept, you'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. `"A26".</p></pre>craictacular: <pre><p>"[A-J][0-9]{1-2}"</p></pre>izuriel: <pre><p>I'd like to pitch in here, just because I don'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 "match the previous value some specific amount of times." These declarations are powerful, you can say things like "match 1 or 2 times" (what you probably wanted) <code>{1,2}</code>, or even "match 1 or more" <code>{1,}</code> (FYI, "one or more" 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 "match a single letter between capital a and capital j."</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 "optimization" if you will. I quoted that because it'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's negation, essentially <code>[^0-9]</code> is <code>\D</code>. The negation doesn'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'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
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传