How would I write a Regex that matches either by email or phone number (phone| email)?

xuanbao · · 564 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Right now I have two Regex strings: </p> <pre><code> //phone number regex r2, _ := regexp.Compile(`1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?`) //Email Regex d2, _ := regexp.Compile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`) </code></pre> <p>When I try and combine the two with an <strong>|</strong> operator it seems to either fall apart, or only one of the two apply. </p> <hr/>**评论:**<br/><br/>therealfakemoot: <pre><p>Ideally, don&#39;t use regular expressions at all. A dedicated parser for each would be ideal. No email address will ever parse as a valid phone number and vice versa.</p> <p>If you absolutely must use regular expressions, write <strong>two</strong>. One for <a href="http://regexlib.com/Search.aspx?k=email" rel="nofollow">email</a> and one for <a href="http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation" rel="nofollow">phone numbers</a>. One will never correctly parse as the other.</p> <p>Trying to meld both into one just adds excessive complexity in terms of debugging and in terms of just understanding what&#39;s going on in the code.</p> <p>As another note, why are your inputs not clearly defined? Why are you in a situation where you aren&#39;t sure if you have a telephone address or a phone number? Maybe you should add some extra logic to the submission form (website or other form of GUI) allowing users to differentiate. You can still do validation but your potential code paths are made much clearer.</p></pre>thisIsExactly20Chars: <pre><p>I second the two regex (if necessary) approach. Also, please don&#39;t write those yourself. They&#39;re plenty of good phone and email regex out there that handle corner cases you might not (and probably won&#39;t) think about unless you spend a day reading the spec and what&#39;s a valid email/phone.</p></pre>TheBeardofGilgamesh: <pre><p>Oh it&#39;s for a web crawler, not an input. </p></pre>metamatic: <pre><p>That&#39;s going to be tough, at least as far as phone numbers go. People are needlessly creative with how they punctuate them.</p> <p>You&#39;ll probably need to do something like <code>\+?[\d\.,\(\)\-]*</code> -- i.e. an optional +, then any number of characters which are either digits or a punctuation character from <code>-(),.</code>.</p></pre>PaulCapestany: <pre><p>This might come in handy: <a href="https://github.com/asaskevich/govalidator/blob/593d64559f7600f29581a3ee42177f5dbded27a9/patterns.go" rel="nofollow">https://github.com/asaskevich/govalidator/blob/593d64559f7600f29581a3ee42177f5dbded27a9/patterns.go</a></p></pre>knotdjb: <pre><p>As <a href="/u/therealfakemoot" rel="nofollow">/u/therealfakemoot</a> said, don&#39;t use regular expressions for parsing, which pretty much echoes <a href="https://commandcenter.blogspot.com.au/2011/08/regular-expressions-in-lexing-and.html" rel="nofollow">the sentiment of commander Pike</a>. So assuming you wrote your own validator/parsers for email and phone number, I would simply do something like this:</p> <pre><code>if strings.ContainsRune(input, &#39;@&#39;) { // code to parse/validate email } else { // code to parse/validate phone number } </code></pre> <p>Edit: Oh look, here&#39;s an <a href="https://golang.org/pkg/net/mail/#ParseAddress" rel="nofollow">email address parser</a>. And here&#39;s a <a href="https://godoc.org/github.com/ttacon/libphonenumber#IsValidNumber" rel="nofollow">phone number validator</a>. Although I do balk at the idea of adding a dependency.</p> <p>Edit: nice catch below about strings.IndexRune returning an int.</p></pre>zeiko_is_back: <pre><p>strings.IndexRune returns an int, not a bool.</p> <pre><code>if len(input) &lt; 3 || !strings.ContainsRune(input, &#39;@&#39;) { // Validation failed } </code></pre> <p>Best practice is to then send an email to that address with a link that can be used to validate the email address.</p></pre>TheBeardofGilgamesh: <pre><p>So what&#39;s so bad about Regex? Is it slow? </p></pre>knotdjb: <pre><p>Basically its overkill and not as precise. The article I linked to elaborates.</p></pre>xiegeo: <pre><p>Now you got two problems. <sup>sorry</sup></p> <p>Try adding more () to group things, like ((phone)|(email)).</p> <p>Also tlds can be more than 4 letters, like <a href="http://www.theverge.com/2013/8/7/4596982/dot-horse-is-here-and-the-internet-will-never-be-the-same" rel="nofollow">.horse</a> or <a href="http://icannwiki.com/.verm%C3%B6gensberatung" rel="nofollow">http://icannwiki.com/.verm%C3%B6gensberatung</a></p></pre>ms4720: <pre><p>Phone Numbers come in all sorts of formats internationally, really a job for a small lexer and parser. </p></pre>

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

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